mindlessgreen
mindlessgreen

Reputation: 12112

Set ggplot theme through a variable

How do I change the theme using a variable?

library(ggplot2)
ggplot(iris,aes(Sepal.Length,Petal.Width))+
  geom_point()

I could do

ggplot(iris,aes(Sepal.Length,Petal.Width))+
  geom_point()+
  theme_bw()

instead what if my theme is defined in a variable,

var1 <- "theme_bw"
var2 <- "theme_grey"

Can I use this to set the theme?

Upvotes: 5

Views: 589

Answers (2)

Khushbu
Khushbu

Reputation: 46

Theme <– "theme_bw()"    
eval(parse(text=as.character(Theme)))

This is another way to do it since I got errors when I used get.

Upvotes: 3

mindlessgreen
mindlessgreen

Reputation: 12112

ggplot() + get("theme_bw")()

Posting baptiste's answer above as I didn't want this question to remain unanswered.

Upvotes: 3

Related Questions