---
title: "Options - styling charts"
author: "Victor Perrier"
date: "`r Sys.Date()`"
output: 
  rmarkdown::html_vignette:
    lib_dir: "billboarder"
vignette: >
  %\VignetteIndexEntry{Options - styling charts}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, echo=FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  screenshot.force = FALSE
)
library("billboarder")
```



## 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) :

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



## Colors

You can specify a new color palette with function `bb_color` :

```{r color_palette}
data("prod_par_filiere")
prod_par_filiere[, c(1, 3, 4, 5, 6, 8)]

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

# 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"))
```

<br/>
Or you can specify each color associated with data with `bb_colors_manual` :

```{r color_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"
  )
```

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

<br/>
For bar charts, you can highlight a value in a simple barchart with :

```{r bar_color_manual}
billboarder() %>% 
  bb_barchart(data = prod_par_filiere[, c(1, 4)], color = "grey") %>% 
  bb_bar_color_manual(values = c("2015" = "firebrick"))
```



## Axis


Add a label to an axis :

```{r axis_label}
# 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"))

```

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

```{r axis_label_format}
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)")
  ))
```

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


```{r axis_label_suffix}
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("%")))
```



<br/>
You can apply a format to x axis as well (especially useful with time), and `fit = FALSE` to don't show all ticks :

```{r xaxis_label_format}
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))
```

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

```{r axis_min}
billboarder() %>% 
  bb_linechart(data = cdc_prod_filiere[, c("date_heure", "prod_solaire")]) %>% 
  bb_y_axis(min = 0, padding = 0)
```




## Legend

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

```{r legend_off}
df <- data.frame(
  cos = cos(seq(-pi, pi, length.out = 30))
)

# No legend
billboarder() %>% 
  bb_linechart(data = df) %>% 
  bb_legend(show = FALSE)
```

<br/>
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`.

```{r legend_name}
billboarder() %>% 
  bb_linechart(data = df) %>% 
  bb_data(names = list(cos = "Cosine"))
```

<br/>
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.

```{r legend_position}
df$sin <- sin(seq(-pi, pi, length.out = 30))

billboarder() %>% 
  bb_linechart(data = df) %>% 
  bb_legend(position = "right")

billboarder() %>% 
  bb_linechart(data = df) %>% 
  bb_legend(position = "inset", inset = list(anchor = "top-left"))
```



## Grids

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

```{r grids}
billboarder() %>% 
  bb_linechart(data = df) %>%
  bb_y_grid(show = TRUE) %>% 
  bb_x_grid(show = TRUE)
```

<br/>
This option also allows you to add vertical and horizontal lines :

```{r hlines}
billboarder() %>% 
  bb_linechart(data = df) %>%
  bb_y_grid(lines = list(
    list(value = 0, text = "Zero")
  ))
```




## Tooltip

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

```{r tooltip_grouped}
billboarder() %>% 
  bb_linechart(data = df) %>%
  bb_tooltip(grouped = FALSE)
```

<br/>
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.

```{r tooltip_format}
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('$,')")
  ))

```





## End

All options combined :

```{r options_all}
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"
  )
```








