5

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?

2
  • 2
    ggplot() + get("theme_bw")() Commented Apr 9, 2017 at 9:46
  • That works. Thanks! Would you like to add it as an answer? Commented Apr 9, 2017 at 11:12

2 Answers 2

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

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

Sign up to request clarification or add additional context in comments.

1 Comment

This method worked for me, whereas get() generated an error message.
3
ggplot() + get("theme_bw")()

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.