billboarder

Victor Perrier

2020-01-08

Examples of charts you can make with billboarder :

Bar chart

Create barcharts with bb_barchart :

library("billboarder")

# data
data("prod_par_filiere")

billboarder(data = prod_par_filiere) %>%
  bb_barchart(
    mapping = bbaes(x = annee, y = prod_hydraulique), color = "#102246"
  ) %>%
  bb_y_grid(show = TRUE) %>%
  bb_y_axis(
    tick = list(format = suffix("TWh")),
    label = list(text = "production (in terawatt-hours)", position = "outer-top")
  ) %>% 
  bb_legend(show = FALSE) %>% 
  bb_labs(
    title = "French hydraulic production",
    caption = "Data source: RTE (https://opendata.rte-france.com)"
  )

To create a dodge barchart, if your data are in โ€œwideโ€ format, use:

library("billboarder")

# data
data("prod_par_filiere")

billboarder() %>%
  bb_barchart(
    data = prod_par_filiere[, c("annee", "prod_hydraulique", "prod_eolien", "prod_solaire")]
  ) %>%
  bb_data(
    names = list(prod_hydraulique = "Hydraulic", prod_eolien = "Wind", prod_solaire = "Solar")
  ) %>% 
  bb_y_grid(show = TRUE) %>%
    bb_y_axis(
    tick = list(format = suffix("TWh")),
    label = list(text = "production (in terawatt-hours)", position = "outer-top")
  ) %>% 
  bb_legend(position = "inset", inset = list(anchor = "top-right")) %>% 
  bb_labs(
    title = "Renewable energy production",
    caption = "Data source: RTE (https://opendata.rte-france.com)"
  )

Same principle for stacked bar charts :

library("billboarder")

# data
data("prod_par_filiere")

# stacked bar chart !
billboarder() %>%
  bb_barchart(
    data = prod_par_filiere[, c("annee", "prod_hydraulique", "prod_eolien", "prod_solaire")], 
    stacked = TRUE
  ) %>%
  bb_data(
    names = list(prod_hydraulique = "Hydraulic", prod_eolien = "Wind", prod_solaire = "Solar"), 
    labels = TRUE
  ) %>% 
  bb_colors_manual(
    "prod_eolien" = "#41AB5D", "prod_hydraulique" = "#4292C6", "prod_solaire" = "#FEB24C"
  ) %>%
  bb_y_grid(show = TRUE) %>%
    bb_y_axis(
    tick = list(format = suffix("TWh")),
    label = list(text = "production (in terawatt-hours)", position = "outer-top")
  ) %>% 
  bb_legend(position = "inset", inset = list(anchor = "top-right")) %>% 
  bb_labs(
    title = "Renewable energy production",
    caption = "Data source: RTE (https://opendata.rte-france.com)"
  )

Scatter plot

A classic one:

billboarder() %>% 
 bb_scatterplot(data = iris, x = "Sepal.Length", y = "Sepal.Width", group = "Species") %>% 
 bb_axis(x = list(tick = list(fit = FALSE))) %>% 
 bb_point(r = 8)

You can make a bubble chart using size aes :

billboarder() %>% 
  bb_scatterplot(
    data = iris, 
    mapping = bbaes(Sepal.Length, Sepal.Width, group = Species, size = Petal.Width)
  ) %>% 
  bb_x_axis(tick = list(fit = FALSE))

Pie charts

Create pie charts :

library("billboarder")

# data
data("prod_par_filiere")
nuclear2016 <- data.frame(
  sources = c("Nuclear", "Other"),
  production = c(
    prod_par_filiere$prod_nucleaire[prod_par_filiere$annee == "2016"],
    prod_par_filiere$prod_total[prod_par_filiere$annee == "2016"] -
      prod_par_filiere$prod_nucleaire[prod_par_filiere$annee == "2016"]
  )
)

# pie chart !
billboarder() %>% 
  bb_piechart(data = nuclear2016) %>% 
  bb_labs(title = "Share of nuclear power in France in 2016",
          caption = "Data source: RTE (https://opendata.rte-france.com)")

You can also do donut charts.

Lines charts

Time serie with Date (and a subchart)

Zoom by dragging

Time serie with POSIXct (and regions)

Stacked area chart

Line range

Use RStudio >= 1.2.0 to display in viewer.

Histogram & density

Create histograms with a numeric vector (or data.frame) :

billboarder() %>%
  bb_histogram(data = rnorm(1e5), binwidth = 0.25) %>%
  bb_colors_manual()

With a grouping variable :

# Generate some data
dat <- data.frame(
  sample = c(rnorm(n = 1e4, mean = 1), rnorm(n = 1e4, mean = 2)),
  group = rep(c("A", "B"), each = 1e4), stringsAsFactors = FALSE
)
# Mean by groups
samples_mean <- tapply(dat$sample, dat$group, mean)
# histogram !
billboarder() %>%
  bb_histogram(data = dat, x = "sample", group = "group", binwidth = 0.25) %>%
  bb_x_grid(
    lines = list(
      list(value = unname(samples_mean['A']), text = "mean of sample A"),
      list(value = unname(samples_mean['B']), text = "mean of sample B")
    )
  )

Density plot with the same data :

billboarder() %>%
  bb_densityplot(data = dat, x = "sample", group = "group") %>%
  bb_x_grid(
    lines = list(
      list(value = unname(samples_mean['A']), text = "mean of sample A"),
      list(value = unname(samples_mean['B']), text = "mean of sample B")
    )
  )