Options - styling charts

Victor Perrier

2024-09-09

Title

Add a title to your chart with bb_title or bb_labs (bb_labs is a shortcut to set title and axis labels at the same time, but with no options for placement) :

billboarder() %>% 
  bb_barchart(table(sample(letters[1:6], 50, TRUE))) %>% 
  bb_title(text = "My title", position = "center")
abcdef01234567891011My titleFreq

Colors

You can specify a new color palette with function bb_color :

data("prod_par_filiere")
prod_par_filiere[, c(1, 3, 4, 5, 6, 8)]
#>   annee prod_therm prod_hydraulique prod_bioenergies prod_eolien prod_solaire
#> 1  2012       48.1             63.8              5.8        14.9          4.1
#> 2  2013       43.6             75.5              7.1        15.9          4.7
#> 3  2014       25.9             68.1              7.5        17.1          5.9
#> 4  2015       34.4             59.1              8.0        21.1          7.4
#> 5  2016       45.9             63.9              8.5        20.7          8.3

# Default
billboarder() %>% 
  bb_barchart(data = prod_par_filiere[, c(1, 3, 4, 5, 6, 8)])
2012201320142015201601020304050607080prod_thermprod_hydrauliqueprod_bioenergiesprod_eolienprod_solaire

# RColorBrewer palette
library("RColorBrewer")
billboarder() %>% 
  bb_barchart(data = prod_par_filiere[, c(1, 3, 4, 5, 6, 8)]) %>% 
  bb_color(palette = brewer.pal(n = 5, name = "Dark2"))
2012201320142015201601020304050607080prod_thermprod_hydrauliqueprod_bioenergiesprod_eolienprod_solaire


Or you can specify each color associated with data with bb_colors_manual :

billboarder() %>% 
  bb_barchart(data = prod_par_filiere[, c(1, 3, 4, 5, 6, 8)]) %>% 
  bb_colors_manual(
    prod_therm = "maroon",
    prod_hydraulique = "royalblue",
    prod_bioenergies = "forestgreen",
    prod_eolien = "plum",
    prod_solaire = "goldenrod"
  )
2012201320142015201601020304050607080prod_thermprod_hydrauliqueprod_bioenergiesprod_eolienprod_solaire

Note : be careful when using named colors, CSS donโ€™t recognize color variant such as royalblue2, firebrick3, โ€ฆ Use HEX code instead.


For bar charts, you can highlight a value in a simple barchart with :

billboarder() %>% 
  bb_barchart(data = prod_par_filiere[, c(1, 4)], color = "grey") %>% 
  bb_bar_color_manual(values = c("2015" = "firebrick"))
2012201320142015201601020304050607080prod_hydraulique

Axis

Add a label to an axis :

# data source : wikipedia
sw <- data.frame(
  film = c("The Force Awakens", "The Phantom Menace",
           "Revenge of the Sith", "A New Hope", 
           "Attack of the Clones", "The Empire Strikes Back", 
           "Return of the Jedi"),
  worldwide_gross = c(2068178225, 1027044677, 848754768,
                      775398007, 649398328, 538375067, 475106177)
)

billboarder() %>% 
  bb_barchart(data = sw) %>% 
  bb_y_axis(label = list(text = "Worldwide grosses", position = "outer-middle"))
The ForceAwakensThe PhantomMenaceRevenge ofthe SithA New HopeAttack ofthe ClonesThe EmpireStrikes BackReturn ofthe JediWorldwide grosses02000000004000000006000000008000000001000000000120000000014000000001600000000180000000020000000002200000000worldwide_gross


You can format values on an axis with JavaScript (use htmlwidgets::JS to mark your character string as literal JavaScript) :

billboarder() %>% 
  bb_barchart(data = sw) %>% 
  bb_y_axis(tick = list(
    values = c(0, 5e+08, 1e+09, 1.5e+09, 2e+09),
    outer = FALSE,
    format = htmlwidgets::JS("d3.formatPrefix('$,.0', 1e6)")
  ))
The ForceAwakensThe PhantomMenaceRevenge ofthe SithA New HopeAttack of theClonesThe EmpireStrikes BackReturn of theJedi$0M$500M$1,000M$1,500M$2,000Mworldwide_gross


If you just want to add a suffix or prefix to the value, use the functions with the same name :

sw2 <- sw
# calculate percentage
sw2$percent <- sw2$worldwide_gross / sum(sw2$worldwide_gross) * 100
sw2$percent <- round(sw2$percent)

sw2$worldwide_gross <- NULL

billboarder() %>% 
  bb_barchart(data = sw2) %>% 
  bb_y_axis(tick = list(format = suffix("%")))
The ForceAwakensThe PhantomMenaceRevenge ofthe SithA New HopeAttack of theClonesThe EmpireStrikes BackReturn of theJedi0%5%10%15%20%25%30%35%percent


You can apply a format to x axis as well (especially useful with time), and fit = FALSE to donโ€™t show all ticks :

data("cdc_prod_filiere")
billboarder() %>% 
  bb_linechart(data = cdc_prod_filiere[, c("date_heure", "prod_solaire")]) %>% 
  bb_x_axis(tick = list(format = "%H:%M", fit = FALSE))
00:0003:0006:0009:0012:0015:0018:0021:000500100015002000250030003500400045005000prod_solaire


Set a minimum on an axis (and look at the difference between above x-axis and below, without fit = FALSE) :

billboarder() %>% 
  bb_linechart(data = cdc_prod_filiere[, c("date_heure", "prod_solaire")]) %>% 
  bb_y_axis(min = 0, padding = 0)
Jun 1212:3001 AM01:3002 AM02:3003 AM03:3004 AM04:3005 AM05:3006 AM06:3007 AM07:3008 AM08:3009 AM09:3010 AM10:3011 AM11:3012 PM12:3001 PM01:3002 PM02:3003 PM03:3004 PM04:3005 PM05:3006 PM06:3007 PM07:3008 PM08:3009 PM09:3010 PM10:3011 PM11:30050010001500200025003000350040004500prod_solaire

Legend

By default, legend is shown, you can hide it with bb_lengend

df <- data.frame(
  cos = cos(seq(-pi, pi, length.out = 30))
)

# No legend
billboarder() %>% 
  bb_linechart(data = df) %>% 
  bb_legend(show = FALSE)
01234567891011121314151617181920212223242526272829-1-0.8-0.6-0.4-0.200.20.40.60.81


You can change the name appearing in the legend with bb_data, by giving an alias to the variable in the data. Here we have a column named cos in our data.frame, we renamed it Cosine.

billboarder() %>% 
  bb_linechart(data = df) %>% 
  bb_data(names = list(cos = "Cosine"))
01234567891011121314151617181920212223242526272829-1-0.8-0.6-0.4-0.200.20.40.60.81Cosine


Legend can be postionned with argument position, three values are possible : "bottom" (the default), "right" and "inset". For the last one, you must specify in which area of the chart the legend must be placed.

df$sin <- sin(seq(-pi, pi, length.out = 30))

billboarder() %>% 
  bb_linechart(data = df) %>% 
  bb_legend(position = "right")
-1-1-1-1-1-1-1-0-0-0-0-0-00.16178199655276460.16178199655276480.37013815533991430.37013815533991460.56118706536238240.72599549192313060.7259954919231310.85685717616758920.85685717616758940.94765317118280250.94765317118280260.9941379571543596-1-0.8-0.6-0.4-0.200.20.40.60.81sin

billboarder() %>% 
  bb_linechart(data = df) %>% 
  bb_legend(position = "inset", inset = list(anchor = "top-left"))
-1-1-1-1-1-1-1-0-0-0-0-0-00.16178199655276460.16178199655276480.37013815533991430.37013815533991460.56118706536238240.72599549192313060.7259954919231310.85685717616758920.85685717616758940.94765317118280250.94765317118280260.9941379571543596-1-0.8-0.6-0.4-0.200.20.40.60.81sin

Grids

You can add grids to a chart with bb_x_axis and bb_y_axis :

billboarder() %>% 
  bb_linechart(data = df) %>%
  bb_y_grid(show = TRUE) %>% 
  bb_x_grid(show = TRUE)
-1-1-1-1-1-1-1-0-0-0-0-0-00.16178199655276460.16178199655276480.37013815533991430.37013815533991460.56118706536238240.72599549192313060.7259954919231310.85685717616758920.85685717616758940.94765317118280250.94765317118280260.9941379571543596-1-0.8-0.6-0.4-0.200.20.40.60.81sin


This option also allows you to add vertical and horizontal lines :

billboarder() %>% 
  bb_linechart(data = df) %>%
  bb_y_grid(lines = list(
    list(value = 0, text = "Zero")
  ))
Zero-1-1-1-1-1-1-1-0-0-0-0-0-00.16178199655276460.16178199655276480.37013815533991430.37013815533991460.56118706536238240.72599549192313060.7259954919231310.85685717616758920.85685717616758940.94765317118280250.94765317118280260.9941379571543596-1-0.8-0.6-0.4-0.200.20.40.60.81sin

Tooltip

You can show the tooltip separately for each serie in the chart :

billboarder() %>% 
  bb_linechart(data = df) %>%
  bb_tooltip(grouped = FALSE)
-1-1-1-1-1-1-1-0-0-0-0-0-00.16178199655276460.16178199655276480.37013815533991430.37013815533991460.56118706536238240.72599549192313060.7259954919231310.85685717616758920.85685717616758940.94765317118280250.94765317118280260.9941379571543596-1-0.8-0.6-0.4-0.200.20.40.60.81sin


You can change the format of the tooltip with a JavaScript function, for example d3.format. Write the function as a character vector, and use htmlwidgets::JS to mark it as literal JavaScript code.

billboarder() %>% 
  bb_barchart(data = sw) %>% 
  bb_tooltip(format = list(
    name =  htmlwidgets::JS("function(name, ratio, id, index) {return 'Worldwide grosses';}"),
    value = htmlwidgets::JS("d3.format('$,')")
  ))
The ForceAwakensThe PhantomMenaceRevenge ofthe SithA New HopeAttack ofthe ClonesThe EmpireStrikes BackReturn ofthe Jedi02000000004000000006000000008000000001000000000120000000014000000001600000000180000000020000000002200000000worldwide_gross

End

All options combined :

billboarder() %>% 
  bb_barchart(data = sw, color = "#CAD5DB") %>% 
  bb_bar_color_manual(values = c("A New Hope" = "#112446")) %>% 
  bb_legend(show = FALSE) %>% 
  bb_y_grid(show = TRUE) %>% 
  bb_y_axis(tick = list(
    values = c(0, 5e+08, 1e+09, 1.5e+09, 2e+09),
    outer = FALSE,
    format = htmlwidgets::JS("d3.formatPrefix('$,.0', 1e6)")
  )) %>% 
  bb_tooltip(format = list(
    name =  htmlwidgets::JS("function(name, ratio, id, index) {return 'Worldwide grosses';}"),
    value = htmlwidgets::JS("d3.format('$,')")
  )) %>% 
  bb_labs(
    title = "Star Wars - Total Lifetime Grosses", 
    y = "Worldwide grosses",
    caption = "Data source : wikipedia"
  )
The ForceAwakensThe PhantomMenaceRevenge ofthe SithA New HopeAttack ofthe ClonesThe EmpireStrikes BackReturn ofthe JediWorldwide grosses$0M$500M$1,000M$1,500M$2,000MStar Wars - Total Lifetime GrossesData source : wikipedia