stackinator
stackinator

Reputation: 5819

ggplot theme_set() only works intermittently

I'll often set a theme early on in a script, with a specific base font size.

# CODE BLOCK 1
library(tidyverse)
ggplot(mtcars, aes(cyl, mpg)) + 
  geom_col() + 
  theme_set(theme_bw(base_size = 20))

Then I'll want to switch the theme and the font size for a different plot later on in my script. I will try to accomplish this with code like below.

# CODE BLOCK 2
ggplot(mtcars, aes(factor(cyl))) + 
  geom_bar() + 
  theme_set(theme_gray(base_size = 6))

I just ran these two code blocks on a fresh R session and when I ran code block 2 the theme and base font size did not change to the arguments listed above. I immediately ran code block 2 again and the theme and font size changed to the arguments shown above. Why is theme_set() only working intermittently?

My session info follows:

OS: Windows 7 64 bit
R: 3.4.4
R Studio: 1.1.442
ggplot2: 2.2.1.9000

Upvotes: 2

Views: 319

Answers (1)

Roman
Roman

Reputation: 17668

Please try this. See also here ?theme_set

theme_set(theme_bw(base_size = 20))
ggplot(mtcars, aes(cyl, mpg)) + 
  geom_col()  

enter image description here

theme_set(theme_gray(base_size = 6))
ggplot(mtcars, aes(factor(cyl))) + 
  geom_bar() 

enter image description here

Or simply without the theme_setfunction

ggplot(mtcars, aes(factor(cyl))) + 
  geom_bar() +
  theme_gray(base_size = 6) 

Upvotes: 3

Related Questions