Creating a Custom ggplot2 Theme 

Nearly every data scientist will say the same thing:

The most important aspect of analytics is effectively communicating your results.

So, how can you do this, whether it be in your practicum project, personal work, or career? The key is to make beautiful and easy-to-interpret visuals. 

The ggplot2 package in R currently provides several options for customizing your figures to give you the best visuals. But these themes still require a bit of tweaking to not only enhance the professionalism of your figures, but also add a bit of personality to stand out. With this in mind, I decided to create a custom theme in R that will take my ggplot2 figures to the next level. 

For this example, I will create a few very simple figures from R’s built-in iris data set, which contains data on the measurements of different iris flowers. A preview of this data set can be seen below: 

Sepal LengthSepal WidthPetal LengthPetal WidthSpecies
5.13.51.40.2setosa
4.93.01.40.2setosa
4.73.21.30.2setosa
4.63.11.50.2setosa
5.03.61.40.2setosa
5.43.91.70.4setosa

I have created a very simple scatter plot to further explain why I avoid using the default ggplot2 themes. Here, R is using the default ggplot2 theme theme_grey().

scatter plot to explain why avoidance for using default ggplot2 themes

While this figure doesn’t look too bad, it still needs a lot of work. Without editing the theme at all, let’s add a facet, axis labels, a title, a subtitle, and a figure caption: 

scatter plot of sepal: petal length for three kinds of flower species

This looks much cleaner than the first figure, but still lacks good aesthetics. Let’s table this figure for now and create a custom theme to take it to the next level. 

There are several starting options when creating a custom ggplot2 theme – you can start from scratch, or start with a completed theme as a template. I found it helpful to start with the complete theme theme_minimal() to streamline the customization process. This theme is a stripped-down version of the ggplot2 default, so it is the perfect template. The code I began with is shown below. Notice the %+replace% operator, which allows the user to overwrite specific elements of the theme without having to start from scratch and without simply adding on to what already exists. 

# ```{r} 

# theme_zach <- function(base_size = 11, base_family = "") { # theme_minimal(base_size = base_size, base_family = base_family) # %+replace% 

# theme( 

# ## TODO: Add theme elements 

# ) 

# } 

# ```

The next part of the design process was to examine different features one at a time. I kept the figure as minimalized and “clean” as possible while making essential details stand out to quickly grab the viewer’s attention and appear professional. I played around with different styles and elements in the figure, including the title, subtitle, caption, axis text, tick marks, gridlines, legend, facets, and many more. Similar to stepwise model selection, I checked piece by piece how the theme looked, adding and removing elements until I was happy with the final product. 

The last thing I wanted to change was R’s default color and figure schemes. The ggplot2 color palette commonly displays red and green, so I wanted to adjust this to be more colorblind friendly. Additionally, I chose to overwrite the ggplot2 default geom_point(), geom_bar(), and geom_col() functions to slightly alter the shape and colors of the points and bars in the figures. 

Now that the new theme is completed, let’s revisit the previous figure and compare it to the same one with the theme added on:

scatter plot with clearer text of sepal: petal length for three kinds of flower species

The difference between the two figures is immediately noticeable, with more appropriately sized text, bolded labels and titles, and an overall cleaner and more professional aesthetic. 

Creating this theme was a small project I had wanted to do for a while.

I can now use this anytime I am working on an assignment, a business report, or even doing exploratory data analysis at the beginning of a project.

Not only did I learn more about ggplot2 and how to customize figure themes, but I also have a quick tool that will set my visuals apart when communicating my results. 

I encourage anyone who creates figures in their daily lives to play around with the customizable features of ggplot2, and also feel free to use my theme! The code for this theme can be found on my GitHub.

I hope you all enjoy using my custom theme, and I highly recommend making your own, even by starting with mine!

Columnist: Zachary Richardson