This package allow you to use billboard.js, a re-usable easy interface JavaScript chart library, based on D3 v4+.
Supported chart types:
The main function is billboarder
, all charts begin with.
You can add layer to your charts with function bb_*
, these
functions correspond to a billboard option define in the API
docs. There are helpers functions to quickly create a type of chart
(bb_barchart
, bb_linechart
,
bb_piechart
, bb_donutchart
,
bb_gauge
, bb_scatterplot
), they have to be
called after billboarder
.
You can create a simple bar chart by passing a
data.frame
to bb_barchart
, the first column
will be used as the x-axis, and the second one as the y-axis :
library("billboarder")
df <- as.data.frame(table(sample(letters[1:5], 50, TRUE)))
df
#> Var1 Freq
#> 1 a 12
#> 2 b 11
#> 3 c 14
#> 4 d 6
#> 5 e 7
billboarder() %>%
bb_barchart(data = df)
If you want to create a grouped bar chart, first option is to put
your data in a “wide” format. Here we use stats::reshape
,
but I recommend to use tidyr::spread
or
data.table::dcast
.
df <- as.data.frame(table(
sample(letters[1:5], 50, TRUE),
sample(LETTERS[1:5], 50, TRUE)
))
df.r <- reshape(data = df, idvar = "Var1", timevar = "Var2", direction = "wide")
df.r
#> Var1 Freq.A Freq.B Freq.C Freq.D Freq.E
#> 1 a 2 3 1 1 3
#> 2 b 2 3 2 6 3
#> 3 c 0 3 0 2 2
#> 4 d 1 2 3 1 2
#> 5 e 2 1 3 2 0
billboarder() %>%
bb_barchart(data = df.r)
Second option is to define a mapping of your variable with function
bbaes
(for more example of mapping, see vignette
billboarder-mapping).
You can pass to the function bb_linechart
a vector, in
that case x-axis will be the index of that vector :
You can change the type of line with argument type
, for
example an area-step
:
If want to specify a variable to map to the x-axis, you had to pass a
data.frame
to the function :
df <- data.frame(
var_x = seq(-pi, pi, length.out = 10),
sin = sin(seq(-pi, pi, length.out = 10))
)
df
#> var_x sin
#> 1 -3.1415927 -1.224647e-16
#> 2 -2.4434610 -6.427876e-01
#> 3 -1.7453293 -9.848078e-01
#> 4 -1.0471976 -8.660254e-01
#> 5 -0.3490659 -3.420201e-01
#> 6 0.3490659 3.420201e-01
#> 7 1.0471976 8.660254e-01
#> 8 1.7453293 9.848078e-01
#> 9 2.4434610 6.427876e-01
#> 10 3.1415927 1.224647e-16
billboarder() %>%
bb_linechart(data = df, x = "var_x")
If the first variable of the data.frame
is a
Date
or a POSIX
, it will be automatically
mapped to the x-axis :
df <- data.frame(
date = seq.Date(from = as.Date("2017-06-12"), by = "day", length.out = 10),
var = rnorm(10)
)
df
#> date var
#> 1 2017-06-12 -1.7378368
#> 2 2017-06-13 0.2528111
#> 3 2017-06-14 -0.3158819
#> 4 2017-06-15 1.8120523
#> 5 2017-06-16 2.1468025
#> 6 2017-06-17 1.0748931
#> 7 2017-06-18 -1.1452601
#> 8 2017-06-19 0.9021934
#> 9 2017-06-20 -0.9942277
#> 10 2017-06-21 -0.8095380
billboarder() %>%
bb_linechart(data = df)
For scatter plot, use a two column data.frame
with
function bb_scatterplot
, or specify the x variable and the
y variable (you can also specify a grouping variable) :
For pie chart, use bb_piechart
with a two column
data.frame
: