Cheatsheets are currently built and used exclusively as a teaching tool. But what if we could produce a cheatsheet that provides a roadmap to build a previously defined product? We could also build the cheatsheet as a function so users can input data into in order to customize it. This provides a consistent format that people can share with each other, and it has the added value of conveying a message through data driven visual changes.
The ggplot2 theme object allows users to specify nearly any part of the plot that is not conditional on the data. What sets the theme object apart is that its structure is consistent while the values in it change. In addition to changing a theme, it is a single function that has a consistent call. The reoccurring challenge for users is to remember all of the options that can be used in the theme call (there are approximately 220 unique options to calibrate at last count). Users may want to bookmark the help page for the theme and remember how you deciphered it last time.
It is incredibly challenging to pass all of the information of the theme to someone who does not know what values are set in your theme and to provide instructions to allow them to easily recreate it.
In writing the library ggedit we tried to make it easy to edit your theme so you don’t have to know too much about ggplots to make many changes at once. For a quick tutorial see here. We wanted to make it easy to track those changes for people who are not versed in R, and plot.theme() was the outcome.
In short, think of the theme as a lot of small images that are combined to create a single portrait:
There are themeType.elements that are not classified in a specific class thus are given values directly, like legend.
To tie this all together we can create this single template that can be replicated for any element in the theme object. To add more information to the output the class of the value given to an elementClassArgument (or a themeType.element) and the index to uniquely identify the element.
plot(theme_get(),themePart = 'legend',fnt=17,plotFrame = F)
We add some colour to distinguish which elements are set to NULL (grey) and which ones have values (red).
As an example this is how to read the output
theme(legend.key=element_rect(fill='grey95',colour='white'), legend.text=element_text(size=rel(0.8)), legend.title=element_text(hjust=0), legend.box.spacing=unit(0.4,'cm'), legend.justification='center', legend.position='right', legend.spacing=unit(0.4,'cm'))
plot(theme_get(),plotFrame = F)
Make this plot interactive by applying ggplotly from the plotly package.
plot(theme_get(),as.plotly = T)
plotFrame argument to the plot call will nest the plots into a generic cheatsheet layout that does a better job of finding the best width for each box and supplies instructions on the border of how to read the output with a caption on the bottom which theme was used.
plot(theme_get(),plotFrame = T,fnt = 10)
Finally there is an option to compare themes. The same layout will be given but the color coding will change, where a blue color will indicate an update from the benchmark theme.
library(ggthemes) plot(obj=theme_economist(),obj2 = theme_bw(),fnt = 10)
When collaborating with many people and large changes are made to the theme this lets you have a single language everyone can understand for quick referencing and hopefully problem solving.
Jonathan Sidi joined Metrum Research Group in 2016 after working for several years on problems in applied statistics, financial stress testing and economic forecasting in both industrial and academic settings.
To learn more about additional open-source software packages developed by Metrum Research Group please visit the Metrum website.