library(tidyverse)
Themes
What are themes?
When creating a plot using ggplot2, other than the actual geometry of the plot itself, you can change the visual features of the plot components using the theme()
function. Within the theme()
function you need to firstly identify the component of the plot that you want to change and the the elements of that plot component.
You can also use a number of preset themes that are summarised below.
Let’s start by loading the tidyverse
packages:
Using ggplot2’s preset themes
First let’s create a plot with no themes applied
<- ggplot(mtcars, aes(wt, mpg)) +
p geom_point() +
ggtitle("Weight vs MPG") +
xlab("Weight (1000 lbs)") +
ylab("Miles per Gallon")
p
Now let’s apply each of ggplot2’s preset themes:
+
pggtitle("bw")+
theme_bw()
+
pggtitle("classic")+
theme_classic()
+
pggtitle("dark")+
theme_dark()
+
pggtitle("light")+
theme_light()
+
pggtitle("linedraw")+
theme_linedraw()
+
pggtitle("minimal")+
theme_minimal()
+
pggtitle("void")+
theme_void()
Customize themes
Using the theme() function you can make specific changes to existing preset themes or built your own theme. Let’s look at a few examples.
Add the theme() function to your plot and then include as your first argument the part of the plot that you want to control e.g. plot.title
. Then specify the type of element that you want to change within that plot part e.g. element_text
This represents a new function which itself has arguments e.g. color = "steelblue"
.
In each of the examples below, various plot components have been changed. If you look at both the code and the output you’ll quickly see how ggplot themes can be used to clearly define the visual look / feel that you want.
Titles, axes, ticks, panel and plot backgrounds
+
ptheme(plot.title =
element_text(color = "steelblue",
family = "Comic Sans MS",
face = "bold",
hjust = 0.5,
size = rel(2)))
+
ptheme(axis.title.x =
element_text(face = "bold.italic",
color = "red",
size = 16),
axis.title.y =
element_text(face = "bold.italic",
color = "red",
size = 16))
+
ptheme(axis.text.x =
element_text(face = "bold.italic",
color = "blue",
size = 22))
+
ptheme(axis.line.x =
element_line(size = 3,
color = "steelblue",
linetype = "dotted"),
axis.line.y =
element_line(size = 3,
color = "salmon",
lineend = "round"))
Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
ℹ Please use the `linewidth` argument instead.
+
ptheme(axis.ticks =
element_line(size = 3,
color = "hotpink"))
%>%
starwars filter(!is.na(homeworld),
< 200) %>%
mass ggplot(aes(x = homeworld, y = mass)) +
geom_boxplot() +
theme_bw()+
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(title = "Mass Distribution by Homeworld in Star Wars",
x = "",
y = "Mass")
+
p theme(aspect.ratio = 0.5)
+
ptheme(plot.background =
element_rect(fill = "lightblue",
color = "white",
size = 5))
Warning: The `size` argument of `element_rect()` is deprecated as of ggplot2 3.4.0.
ℹ Please use the `linewidth` argument instead.
+
p theme(panel.background =
element_rect(fill = "lightyellow",
color = "lightblue",
size = 5))
+
ptheme(panel.grid.major.x =
element_line(size = 3,
color = "steelblue"),
panel.grid.minor.y =
element_line(color = "darkblue"))
Legends
<- ggplot(mtcars, aes(wt, mpg, color = factor(cyl))) +
p_legend geom_point() +
labs(title = "Weight vs MPG by Cyl",
x = "Weight of cars",
y = "Miles per gallon",
color = "Cylinders")
p_legend
+
p_legend theme(legend.box.margin = margin(50, 15, 30, 5),
legend.box.background =
element_rect(fill = "lightblue",
color = "black",
size = 1))
+
p_legend theme(legend.background =
element_rect(fill = "lightyellow",
color = "black",
size = 0.5))
+
p_legend theme(legend.background =
element_rect(fill = "lightyellow",
color = "black",
size = 0.8),
legend.key =
element_rect(fill = "lightyellow",
color = "black",
size = 0.2))
+
p_legend theme(legend.background =
element_rect(fill = "lightyellow",
color = "black",
size = 0.8),
legend.key =
element_rect(fill = "lightyellow",
color = "black",
size = 0.2),
legend.key.size = unit(1, "cm"),
legend.title =
element_text(face = "bold.italic"),
legend.text =
element_text(family = "Comic Sans MS",
face = "bold",
color = "steelblue"))
+
p_legend theme(
legend.key =
element_rect(fill = "white"),
legend.position = "bottom")
+
p_legend theme(panel.background =
element_rect(fill = "white"),
plot.background =
element_rect(fill = "lightgrey"),
legend.background =
element_rect(color = "grey"),
legend.position = c(0.9, 0.8))
Warning: A numeric `legend.position` argument in `theme()` was deprecated in ggplot2
3.5.0.
ℹ Please use the `legend.position.inside` argument of `theme()` instead.
Facets
<- ggplot(mtcars, aes(wt, mpg)) +
p_facet geom_point() +
facet_wrap(~cyl) +
ggtitle("Weight vs MPG by Cylinders")
p_facet
+
p_facet theme(strip.background =
element_rect(fill = "lightblue",
color = "steelblue",
size = 2),
strip.text =
element_text(size = 14,
face = "bold",
color = "steelblue"))
p_facet
+
p_facet theme(panel.spacing =
unit(2, unit = "lines"))
+
p_facet theme(panel.grid.major =
element_line(color = "black",
size = 0.5),
panel.grid.minor =
element_line(color = "steelblue",
size = 0.3))
Setting the theme
Using the theme_set()
function you can set your theme parameters at the beginning of your code and it will be applied to all plots thereafter.
theme_set(
theme(plot.title =
element_text(size = 15,
face = "bold",
family = "Comic Sans MS",
color = "steelblue4",
hjust = 0.5),
panel.background =
element_rect(fill = "white"),
plot.background =
element_rect(fill = "lightsteelblue2")))
p