BBC Visual and Data Journalism cookbook for R graphics

How to create BBC style graphics

At the BBC data team, we have developed an R package and an R cookbook to make the process of creating publication-ready graphics in our in-house style using R’s ggplot2 library a more reproducible process, as well as making it easier for people new to R to create graphics.

The cookbook below should hopefully help anyone who wants to make graphics like these:

We’ll get to how you can put together the various elements of these graphics, but let’s get the admin out of the way first…

Load all the libraries you need A few of the steps in this cookbook - and to create charts in R in general - require certain packages to be installed and loaded. So that you do not have to install and load them one by one, you can use the p_load function in the pacman package to load them all at once with the following code. #This line of code installs the pacman page if you do not have it installed - if you do, it simply loads the package if(!require(pacman))install.packages("pacman") pacman::p_load('dplyr', 'tidyr', 'gapminder', 'ggplot2', 'ggalt', 'forcats', 'R.utils', 'png', 'grid', 'ggpubr', 'scales', 'bbplot')

Install the bbplot package bbplot is not on CRAN, so you will have to install it directly from Github using devtools . If you do not have the devtools package installed, you will have to run the first line in the code below as well. # install.packages('devtools') devtools::install_github('bbc/bbplot') For more info on bbplot check out the package’s Github repo, but most of the details about how to use the package and its functions are detailed below. When you have downloaded the package and successfully installed it you are good to go and create charts.

How does the bbplot package work? The package has two functions, bbc_style() and finalise_plot() . bbc_style() : has no arguments and is added to the ggplot ‘chain’ after you have created a plot. What it does is generally makes text size, font and colour, axis lines, axis text, margins and many other standard chart components into BBC style, which has been formulated based on recommendations and feedback from the design team. Note that colours for lines in the case of a line chart or bars for a bar chart, do not come out of the box from the bbc_style() function, but need to be explicitly set in your other standard ggplot chart functions. The code below shows how the bbc_style() should be used within standard chart-production workflow. This is an example for a very simple line chart, using data from the gapminder package. #Data for chart from gapminder package line_df <- gapminder %>% filter(country == "Malawi") #Make plot line <- ggplot(line_df, aes(x = year, y = lifeExp)) + geom_line(colour = "#1380A1", size = 1) + geom_hline(yintercept = 0, size = 1, colour="#333333") + bbc_style() + labs(title="Living longer", subtitle = "Life expectancy in Malawi 1952-2007") Here is what the bbc_style() function actually does under the hood. It essentially modifies certain arguments in the theme function of ggplot2 . For example, the first argument is setting the font, size, typeface and colour of the title element of the plot. ## function () ## { ## font <- "Helvetica" ## ggplot2::theme(plot.title = ggplot2::element_text(family = font, ## size = 28, face = "bold", color = "#222222"), plot.subtitle = ggplot2::element_text(family = font, ## size = 22, margin = ggplot2::margin(9, 0, 9, 0)), plot.caption = ggplot2::element_blank(), ## legend.position = "top", legend.text.align = 0, legend.background = ggplot2::element_blank(), ## legend.title = ggplot2::element_blank(), legend.key = ggplot2::element_blank(), ## legend.text = ggplot2::element_text(family = font, size = 18, ## color = "#222222"), axis.title = ggplot2::element_blank(), ## axis.text = ggplot2::element_text(family = font, size = 18, ## color = "#222222"), axis.text.x = ggplot2::element_text(margin = ggplot2::margin(5, ## b = 10)), axis.ticks = ggplot2::element_blank(), ## axis.line = ggplot2::element_blank(), panel.grid.minor = ggplot2::element_blank(), ## panel.grid.major.y = ggplot2::element_line(color = "#cbcbcb"), ## panel.grid.major.x = ggplot2::element_blank(), panel.background = ggplot2::element_blank(), ## strip.background = ggplot2::element_rect(fill = "white"), ## strip.text = ggplot2::element_text(size = 22, hjust = 0)) ## } ## <environment: namespace:bbplot> You can modify these settings for your chart, or add additional theme arguments, by calling the theme function with the arguments you want - but please note that for it to work you must call it after you have called the bbc_style function. Otherwise bbc_style() will override it. This will add some gridlines, by adding extra theme arguments to what is included in the bbc_style() function. There are many similar examples throughout the cookbook. theme(panel.grid.major.x = element_line(color="#cbcbcb"), panel.grid.major.y=element_blank())