You should use theme_set() in addition to theme() in ggplot
theme_set()
takes theme objects as its argument, and it will apply it to all subsequent ggplots. For example, you can do theme_set(theme_bw())
and it’ll apply theme_bw()
to all your ggplots. I personally like cowplot::theme_cowplot() as a starting theme to modify.
You can also use ggplot-style addition to add multiple theme objects (and theme_set()
will apply them all to the ggplots.
Helpful for consistency across all your figures - no more copy and pasting theme()
s.
library(ggplot2)
ggplot2::theme_set(cowplot::theme_cowplot(line_size = 0.25, font_size = 10) +
theme(axis.ticks.length = unit(-3, "pt"),
legend.title = element_blank()))
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) +
geom_point(aes(fill = Species, color = Species, shape = Species),
stroke = 0.25,
size = 3) +
scale_shape_manual(values = c(21, 23, 24)) +
scale_fill_viridis_d(alpha = 0.5) +
scale_color_viridis_d(alpha = 0.8) +
scale_x_continuous(limits = c(-0.5,9), breaks = seq(0, 9, 3)) +
scale_y_continuous(limits = c(-0.5,9), breaks = seq(0, 9, 3)) +
coord_cartesian(expand = FALSE) +
theme(legend.position = c(0.1, 0.85),
legend.text = element_text(face = "italic"))