knitr::opts_chunk$set( collapse = TRUE, comment = "#>", screenshot.force = FALSE ) library("billboarder")
There's 3 ways with billboarder
to pass data to construct a chart :
data.frame
with only variable that need to be used, typically for a barchart, the first column is used for x-axis, the second for y-axis.billboarder
and use function bb_aes
in a "pipe" flow.bbaes
inside chart builder helper such as bb_barchart
, bb_linechart
, ...We'll use data about the french electricity production between 2012 and 2016 :
data("prod_par_filiere") str(prod_par_filiere)
data.frame
For creating this simple barchart, we need to use two columns of our data.frame
billboarder() %>% bb_barchart(data = prod_par_filiere[, c("annee", "prod_bioenergies")])
Variable annee
is used on the x-axis, and prod_bioenergies
as y values.
This is similar for line chart :
billboarder() %>% bb_linechart(data = prod_par_filiere[, c("annee", "prod_bioenergies")])
We can pass our data to function billboarder
and then call bb_aes
to specify which variable to use :
billboarder(data = prod_par_filiere) %>% bb_aes(x = annee, y = prod_bioenergies) %>% bb_barchart()
You don't have to pass arguments to bb_barchart
.
This is the same for line chart :
billboarder(data = prod_par_filiere) %>% bb_aes(x = annee, y = prod_bioenergies) %>% bb_linechart()
Mapping can be specified inside the function which specify the type of chart :
billboarder(data = prod_par_filiere) %>% bb_barchart(mapping = bbaes(x = annee, y = prod_bioenergies))
The function to map variables is bbaes
without underscore.
For line chart :
billboarder(data = prod_par_filiere) %>% bb_linechart(mapping = bbaes(x = annee, y = prod_bioenergies))
Construct a chart with groups differ between the first method and the others. For the first one, data need to be in 'wide' format, the other need data in 'long' format with a grouping variable.
With 'wide' data :
billboarder() %>% bb_barchart( data = prod_par_filiere[, c("annee", "prod_bioenergies", "prod_eolien", "prod_solaire", "prod_hydraulique")] )
with 'long' data :
# prepare data data("prod_filiere_long") prod_filiere_long <- prod_filiere_long[ prod_filiere_long$branche %in% c("bioenergies", "eolien", "solaire", "hydraulique"), ] head(prod_filiere_long) billboarder(data = prod_filiere_long) %>% bb_barchart(mapping = bbaes(x = annee, y = prod, group = branche))
In Shiny app or in function, you can use bbaes_string
or bb_aes_string
, these function accept character instead of unquoted variable names :
billboarder(data = prod_filiere_long) %>% bb_barchart(mapping = bbaes_string(x = "annee", y = "prod", group = "branche"))
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.