Table of Contents
A comprehensive guide to the best ggplot2 themes is a complete and detailed resource that provides guidance and recommendations on how to enhance the visual appearance of graphs and charts created using the ggplot2 package in R. It includes a curated list of the most effective and visually appealing themes, along with step-by-step instructions and code examples on how to implement them in ggplot2. This guide aims to help users create professional-looking and visually appealing graphics for data analysis and presentation purposes. It also provides insights and tips on how to choose and customize themes to suit different data types and purposes. Overall, this guide serves as a valuable reference for anyone looking to improve the aesthetics of their ggplot2 graphs and charts.
A Complete Guide to the Best ggplot2 Themes
This tutorial provides a complete guide to the best ggplot2 themes, including:
- How to modify the appearance of plots using built-in ggplot2 themes.
- How to modify the appearance of plots using predefined themes from the ggthemes library.
- How to modify specific components of the theme including the plot panel background and the gridlines.
How to Modify Plot Appearance Using Built-in ggplot2 Themes
For each of the following examples, we’ll use the built-in R dataset iris:
#view first six rows of iris dataset
head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
First, we’ll load the ggplot2 library and create a that shows Sepal.Length on the x-axis and Sepal.Width on the y-axis, colored according to Species:
#load ggplot2 library library(ggplot2) #create scatterplot ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) + geom_point()
Next, we’ll show how each of the built-in ggplot2 themes impact the appearance of the plot.
theme_gray
The default theme, featuring a gray background and white gridlines.
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point() +
theme_gray()
theme_bw
A black on white theme.
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point() +
theme_bw()
theme_linedraw
A theme with only black lines of various widths on white backgrounds.
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point() +
theme_linedraw()
theme_light
A theme similar to theme_linedraw but with grey lines and axes designed to draw more attention to the data.
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point() +
theme_light()
theme_dark
A theme similar to theme_light, but with a dark background. A useful theme for making thin colored lines stand out.
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point() +
theme_dark()
theme_minimal
A theme with no background annotations.
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point() +
theme_minimal()
theme_classic
A theme with no gridlines.
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point() +
theme_classic()
theme_void
A completely empty theme.
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point() +
theme_void()
How to modify the appearance of plots using predefined themes from the ggthemes library
In addition to using the built-in ggplot2 themes, we can use the predefined themes from the ggthemes library to modify the aesthetics of plots.
First, we’ll load the ggthemes library:
library(ggthemes)
Next, we’ll show a few examples of how to use the predefined themes to modify the aesthetics of plots:
theme_wsj
A Wall Street Journal theme.
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point() +
theme_wsj()
theme_tufte
A minimalist theme inspired by the work of statistician Edward Tufte.
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point() +
theme_tufte()
theme_solarized
A theme that uses colors based on .
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point() +
theme_solarized()
Note that we can also use the argument light = FALSE to use a dark background on the plot:
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point() +
theme_solarized(light = FALSE)
theme_gdocs
A theme with Google Docs Chart defaults.
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point() +
theme_gdocs()
theme_fivethirtyeight
Theme inspired by plots.
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point() +
theme_fivethirtyeight()
theme_economist
Theme inspired by The Economist.
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point() +
theme_economist()
How to Modify Specific Components of Plots
We can use the theme() and element_rect() functions to change the plot panel background color:
theme(panel.background = element_rect(fill, color, size))
- fill: fill color for rectangle
- color: border color
- size: border size
We can also use the element_line() function to change the size and appearance of the gridlines:
theme(panel.grid.major = element_line(color, size, linetype), panel.grid.minor = element_line(color, size, linetype))
- color: border color
- size: border size
- linetype: line type (“blank”, “solid”, “dashed”, “dotted”, “dotdash”, “longdash”, “twodash”)
The following code illustrates how to remove the plot panel borders and the gridlines:
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point() +
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
The following code illustrates how to modify the plot panel background and the gridlines:
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) + geom_point() + theme( panel.background = element_rect(fill = "powderblue", color = "powderblue", size = 0.5, linetype = "solid"), panel.grid.major = element_line(size = 0.5, linetype = 'solid', color = "white"), panel.grid.minor = element_line(size = 0.25, linetype = 'solid', color = "white") )