How to remove quotes from string to use in theme_set() for ggplot

Viewed 46

I have a several names of themes for ggplot stored in a csv file and I want to use them to set themes in ggplot. However, as expected, the themes are stored with quotation marks and I need to remove them in order to use them with the function theme_set(). I have used the functions cat(), noquote() and print(, quote = FALSE) but they haven't work. For example, I have tried this:

a <- "theme_economist()"
theme_set(noquote(a))
theme_set(cat(a,"\n"))
theme_set(print(a, quote = FALSE))

But none of these have worked with ggplot.

Thanks in advance

1 Answers

Using parse and eval functions seem to work.
You can use it in a loop.

### Libraries
library(ggplot2)
library(ggthemes)

### Storing theme
a <- "theme_economist()"

### Example
df <- data.frame(val=1:3, group=c('a', 'b', 'c'))
ggplot(df, aes(x=group, y=val)) + 
  eval(parse(text=a))
Related