A Sample of Themes to Help in Selecting the Right One
Sooner or later, the times comes to move beyond the default ggplot2 grey theme and explore other options. But which theme to use? And When?
Here is a collection of ggplot charts with different themes applied, intended to provide some helpful comparisons and make it easier to choose the best option for the situation.
The focus is on themes that come with the ggplot package, but also included are some examples from ‘ggthemes’ and ‘ggpubr’ packages. Consideration could be extended to other packages like ‘hrbrthemes’, ‘ggthemr’, ‘themetron’ or others. Although the possibilities are endless, realistically, many of the themes are variations on existing themes/concepts and it may be just as easy to work with a built-in theme and tweak it to your own desired flavour. Some examples and thoughts on this at the end.
Theme sources used:
ggplot2 built-in: if none of the below specified.
ggthemes library: identified by ‘ggt’ in chart titles below.
ggpubr library: identified by ‘ggp’ in chart titles below.
the ‘transparent’ theme suppresses title by default, so extra modification was needed. if used ‘out of the box’, will not have a title, even if title is specified.
Colors:
Default ggplot colors used for expediency - exploring color palettes is another adventure altogether.
First, the data
Using global_economy data set from tsibbledata package because it is characteristics of data I typically work with:
multiple measures on different scales, for variety
Of course, this is not exhaustive exploration of the variations, but intended as an illustrative cross-section.
Code
# data set with time dimension, at least one other dimension, at least one metricchart_data <- tsibbledata::global_economy# add metric for GDP per capitachart_data <- chart_data %>%mutate(GDP_per_cap = GDP / Population)# isolate country for use casechart_data_single <- chart_data %>%filter(Code=='CAN')# filter for select countries to make manageable for use caseschart_data_select <- chart_data %>%filter(Code %in%c("CAN","USA","BRA","JPN","IND","NGA"))
Function to take chart info and process themes
A reusable function for ease of sampling different themes with different chart types.
# set up chart for applying themeschart_example <- chart_data_select %>%ggplot(aes(x=Growth))+geom_histogram()# use function to apply themes to the charts and displaychartThemeSampler(chart_example)
Clearly, there are lots of options readily available to suit most situations. All of these themes have their strengths and limitations.
Still, it is entirely possible that none of the ‘out-of-the-box’ themes may fit exactly with what you want. Here are some examples of minor modifications to built-in themes to see how they play out across different chart types.
Chart Setup & Function for mods
Chart setup
A collection of charts to apply themes to, as opposed to applying various themes to a specific chart, as above.
# takes list of chart settings plus additional theme definition as inputs and combineschartMod <-function(charts, chart_mods, theme_def) {for(c in1:length(charts)) { chart_modo <- charts[[c]] + chart_mods + theme_defprint(chart_modo) }}
Light without grid lines
theme_light, with border
no gridlines
geom_hline for line at 0 on y-axis
# set list of chartscharts <-list(chart_line, chart_col, chart_scat, chart_box, chart_col_facet, chart_scat_facet)# add chart modificationschart_mods <-geom_hline(yintercept =0, color='grey90')# set theme to applytheme_def <-theme_light() +theme(panel.grid =element_blank())# run function to show chartschartMod(charts, chart_mods, theme_def)
Light wth major gridlines only
Major gridlines for reference with color lightened and size reduced. 0 line for extra context, distinguished from gridlines.
consider your objectives, audience and priorities:
if time is of the essence, a built-in chart or even default may be good enough
if design is important to your audience, you may want to go the extra distance to craft something more specialized
if looking to create a personal or professional brand, may want to create your own template that you can use repeatedly over time and different situations
keep things as simple as possible, but no simpler: provide enough guidance to direct and assist the eye
consider reference lines in addition to / instead of gridlines to provide easier anchoring
Above all else, for the sake of your data consumers…keep on visualizing!