0

I struggled with this problem for a long time. Say, I have 2 plots and want to export them using ggexport().

library(ggplot2)

p1 <- qplot(data = mtcars, x = cyl)
p2 <- qplot(data = mtcars, x = cyl, y = mpg)

plot_list <- list(p1, p2)
ggexport(plotlist = plot_list, filename = 'test.pdf')

Now I have many of plots named p1,p2,p3...p100 and I don't want to type them one by one, so I use paste0() (at this example I only paste p1 and p2)

plot_list <- list(paste0('p',1:2))

It's just a character vector in plot_list, not the plots. I searched for some methods and tried but all failed.

plot_list <- list(parse(paste0('p',1:2)))

plot_list <- list(eval(parse(paste0('p',1:2))))

plot_list <- list(syms(paste0('p',1:2)))

# only one plot in it
plot_list <- list(get(paste0('p',1:2)))

Could someone tell me how to do this or support some reference. I think I have a lot of deficiencies in the object name selecting and using.

Any help will be highly appreciated!

zhiwei li
  • 1,635
  • 8
  • 26

1 Answers1

1

Using lapply to apply the get on your character vector of plot names does the trick of returning a list of plots

plot_list <- lapply(paste0('p',1:2), get)
waskuf
  • 415
  • 2
  • 4