Function equivalent to (python) seaborn's "set_context()" in (R) ggplot2?

Question:

A quite neat function of python’s library seaborn is to be able to all the sizes of the plots, labels and the majority of graph elements with a single command: set_context(context), for different contexts the sizes of figures are resized accordingly, so if context is talk everything is larger, but for paper they are scaled down so there is space to include more information in a single graph.

Is there a function equivalent to set_context() in ggplot2?

currently, I am using many theme instructions, such as:

...
theme(text = element_text(size=14)) +
...

and hardcoding all the sizes in the code

Asked By: Rafael Menezes

||

Answers:

I’m not too familiar with seaborn, but based on your description I think there are two features in {ggplot2} that might suit your needs.

If you want all your plots to use the same theme, you can run theme_set() at the top of your script/document to use the same theme for all your plots. Here you can declare a specific "complete theme" and then use base_size to set a default font size. So I often use theme_set(theme_bw(base_size = 16)) at the top of many of my scripts to avoid retyping that for each plot.

If you want to apply different complex themes to different plots, you can just save each theme to a variable and call it as needed.

library(ggplot2)

# create custom themes and assign to variable
theme_talk <- theme(text = element_text(size = 14))
theme_paper <- theme(text = element_text(size = 8))

p <- ggplot(mtcars, aes(disp, mpg)) + geom_point()

# call custom themes as needed
p + ggtitle("Talk theme has big text") + theme_talk

p + ggtitle("Paper theme has small text") + theme_paper

Created on 2022-11-23 with reprex v2.0.2

Answered By: Dan Adams
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.