Closed Captioning Closed captioning available on our YouTube channel

How to customize ggplot with bbplot

InfoWorld | Apr 12, 2019

Overwhelmed by all the ways you can customize a ggplot2 data visualization? With add-on packages like the BBC’s bbplot, you can use themes created by design professionals – or tweak them to make them your own.

Copyright © 2019 IDG Communications, Inc.

Hi. I’m Sharon Machlis at IDG Communications, here with Do More With R episode 25: Customize ggplot with bbplot.
One of the great things about the ggplot2 package is how powerful and flexible it is. There are so many things you can customize in a step-by-step process: colors, fonts, grid lines …. But if you’re not a design professional, all those choices can be a bit overwhelming.
Good news is, there are skilled designers who’ve created ggplot themes. You can either use one of those as-is, or add your own tweaks to a theme and make it your own.
The BBC use R and ggplot for some of their graphics. The BBC data team created their own ggplot theme, and then kindly posted the code on GitHub so all of us can use it too. They even published a “Cookbook” online explaining how to use it. That’s at bbc dot github dot io slash rcookbook. So let’s head over there.
Let’s look at the simple linechart code example in the cookbook It will be this, using gapminder data from the gapminder package.
Bbplot isn’t on CRAN, so you’ll need to use a package like devtools, remotes, or pacman to install it.
BBC uses pacman to load packages in its cookbook, so I will, too. That second line of code will install the bbplot package from the bbc’s bbplot GitHub repository if it’s not on your system. If it is on your system, that code just loads the package if it’s already there. The first line installs any packages that aren’t already on your system, and just loads them if they’re already there.
Now I’ll create the first basic line chart in the BBC cookbook, but using ggplot2 defaults.
Next, look what happens to that graph if I add the bbc_style() function from bbplot:
Definitely a different style!
We can look at what’s going on under the hood by running bbc_style without the parentheses.
But for better code format, go to the bbplot GitHub repo and find the R file.
I’m going to, and then the R folder, and opening the bbc_style.R file.
I’ll click the raw button and copy and paste into a new file.
The code is nicely commented. I can make my own, slightly modified personal function based on this, with a couple of my own tweaks
Say, for an easy example, I’d like my plot title and subtitle centered. I’ll turn bbc_style into my own new function – I’ll call it my_tweaks_bbc_style – and then add a little code.
The code to center text in ggplot2 is hjust = 0.5 in the element_text function. I’ll add that
Now I’ll source this function
and then run it on my line chart.
And you see, my title and subtitle are now centered.
There’s obviously a lot more in here you can tweak. Do your tweaks, and then save your function. You can save it in a file that you source whenever creating visualizations. Maybe in your own GitHub repo or other public file so your code is reproducible by others. Or you can add a function like that to be part of your own personal R package.
Don’t know how to create your own R package? Check out Episode 22 of Do More With R: Write Your Own R Package, either at or on YouTube’s IDG Tech Talk channel.
And that’s it for this episode, thanks for watching! For more R tips, head to the Do More With R page at go dot infoworld dot com slash more with R, all lowercase except for the R.
You can also find the Do More With R playlist on the YouTube IDG Tech Talk channel.
Hope to see you next episode!
Featured videos from