Themes
With the ‘theme’-argument (theme()
) you can adjust (nearly) all appearence features of the graph. ggplot also provides some themes that can give you an impression of what is possible.
Standard themes
The standard themes of ggplot include: theme_bw
, theme_gray
, theme_dark
, theme_classic
, theme_minimal
, theme_linedraw
, theme_void
<- ggplot(mpg, aes(x = displ, y = cty, colour = drv)) + geom_point() +
regular labs(title = "standard theme")
<- regular + theme_bw() + labs(title = "theme_bw")
bw <- regular + theme_gray() + labs(title = "theme_gray")
gray <- regular + theme_dark() + labs(title = "theme_dark")
dark <- regular + theme_classic() + labs(title = "theme_classic")
classic <- regular + theme_minimal() + labs(title = "theme_minimal")
minimal <- regular + theme_linedraw() + labs(title = "theme_linedraw")
linedrw <- regular + theme_void() + labs(title = "theme_void") void
Showing multiple figures at once takes some effort:
# install.packages("devtools")
# devtools::install_github("thomasp85/patchwork")
library(patchwork)
+ bw + gray + dark + classic + minimal + linedrw + void + plot_layout(ncol = 4) regular
Customized themes
You can really change a lot of features from the graph, allowing you to create the exact graph you had in mind. Look at all the options within theme (plus an explanation of each aspect here: http://ggplot2.tidyverse.org/reference/theme.html)
theme(line, rect, text, title, aspect.ratio, axis.title, axis.title.x,
axis.title.x.top, axis.title.y, axis.title.y.right, axis.text, axis.text.x,
axis.text.x.top, axis.text.y, axis.text.y.right, axis.ticks, axis.ticks.x,
axis.ticks.y, axis.ticks.length, axis.line, axis.line.x, axis.line.y,
legend.background, legend.margin, legend.spacing, legend.spacing.x,
legend.spacing.y, legend.key, legend.key.size, legend.key.height,
legend.key.width, legend.text, legend.text.align, legend.title,
legend.title.align, legend.position, legend.direction, legend.justification,
legend.box, legend.box.just, legend.box.margin, legend.box.background,
legend.box.spacing, panel.background, panel.border, panel.spacing,
panel.spacing.x, panel.spacing.y, panel.grid, panel.grid.major,
panel.grid.minor, panel.grid.major.x, panel.grid.major.y, panel.grid.minor.x,
panel.grid.minor.y, panel.ontop, plot.background, plot.title, plot.subtitle,
plot.caption, plot.margin, strip.background, strip.placement, strip.text,
strip.text.x, strip.text.y, strip.switch.pad.grid, strip.switch.pad.wrap, ...,complete = FALSE, validate = TRUE
)
Let’s change some features:
ggplot(mpg, aes(x = displ, y = cty, colour = drv)) + geom_point() +
theme(
axis.title = element_text(size = 18, face = "bold"),
legend.position = "top",
plot.background = element_rect("grey"),
panel.background = element_rect("white"),
panel.grid.major = element_line(size = 1, linetype = "dashed", colour = "darkgrey"),
legend.background = element_rect(colour = "orange", fill = "green")
)
What might be handy to realize is that you can also store your theme options and use them: Let’s change some features:
<- theme(
my_theme axis.title = element_text(size = 18, face = "bold"),
legend.position = "top",
plot.background = element_rect("grey"),
panel.background = element_rect("white"),
panel.grid.major = element_line(size = 1, linetype = "dashed", colour = "darkgrey"),
legend.background = element_rect(colour = "orange", fill = "green")
)
ggplot(mpg, aes(x = displ, y = cty, colour = drv)) + geom_point() + my_theme
Other themes
We can also use themes that were made by others. For instance, by making use of the package ggthemes
we can recreate graphs in the style of “The economist”, or, “Wall Street Journal”, or old awful excel graphs. Let’s see some cool examples:
# install.packages("ggthemes")
library(ggthemes)
<- ggplot(mpg, aes(x = displ, y = cty, colour = drv)) + geom_point() +
regular labs(title = "standard theme")
<- regular + theme_economist() + labs(title = "theme_economist")
econ <- regular + theme_excel() + labs(title = "theme_excel")
excel <- regular + theme_fivethirtyeight() + labs(title = "theme_fivethirtyeight")
fte <- regular + theme_tufte() + labs(title = "theme_tufte")
tufte <- regular + theme_wsj() + labs(title = "theme_wsj")
wsj
+ econ + excel + fte + tufte + wsj + plot_layout(ncol = 3) regular
There are also more fun themse such as tv-themes:
# install.packages("tvthemes")
library(tvthemes)
# install.packages("extrafont")
library(extrafont)
## Registering fonts with R
+
regular scale_colour_spongeBob() +
theme_minimal() +
labs(title = "Spongebob colours")
Or dark themes:
# install.packages("ggdark")
library(ggdark)
+
regular dark_theme_gray() +
labs(title = "dark theme")
invert_geom_defaults()