Skip to content

Instantly share code, notes, and snippets.

@clairemcwhite
Last active January 19, 2021 01:04
Show Gist options
  • Save clairemcwhite/733f62522d3ccc9920bb5cbe5fdfda1b to your computer and use it in GitHub Desktop.
Save clairemcwhite/733f62522d3ccc9920bb5cbe5fdfda1b to your computer and use it in GitHub Desktop.
Consistent exported font sizes
# Only needed if using theme_cowplot. Otherwise, use any theme.
library(cowplot)
theme_cowplot_consistent_text <- function (font_size = 8) {
theme_cowplot() %+replace%
theme(strip.text = element_text(size = font_size),
axis.text = element_text(colour = "black", size = font_size),
plot.title = element_text(size = font_size),
axis.title = element_text(size = font_size),
legend.text = element_text(size = font_size),
legend.title = element_text(size = font_size),
legend.key.size = unit(0.5, "lines"),
axis.title.x = element_text(margin = ggplot2::margin(t = 0, r = 0, b = 0, l = 0), vjust = 1),
axis.text.x = element_text(margin = ggplot2::margin(t = 1, r = 0, b = 0, l = 0), vjust = 1),
axis.title.y = element_text(margin = ggplot2::margin(t = 0, r = 0, b = 0, l = 0), angle = 90, vjust = 1),
axis.text.y = element_text(margin = ggplot2::margin(t = 0, r = 1, b = 0, l = 0), hjust = 1)
)
}
theme_set(theme_cowplot_consistent_text(font_size = 8))
@clairemcwhite
Copy link
Author

clairemcwhite commented Feb 19, 2020

This is the theme I use to get all pdf images exported with ggsave to have font size 8 for all text elements in the plot.

The theme doesn't seem to carry through to ggrepel, so use geom_text_repel(size = 2.5) to get about size 8 font. (size = 2 for about 6 pt).

Font will be size eight when exported as in:
plt %>% ggsave("figures/plot.pdf", ., device = "pdf", width = 3, height = 1.75, units = "in")

To load it do:
library(cowplot)
theme_set(theme_cowplot_consistent_font(size = 8))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment