我正在尝试使用for循环打印输出并保存到ggplot2对象(使用适当的名称,p.vec的每个元素可能会发生变化),任何帮助都将不胜感激,非常感谢。
par(mfrow = c(2, 2))
p.vec <- c("disp","mpg")
for(i in p.vec){
p <- ggplot(data = mtcars, aes(x = mpg, y= i)) +geom_jitter()
print(p)
#ggsave("plot.pdf")
}
发布于 2020-10-13 12:26:20
尝试使用lapply
:
library(ggplot2)
p.vec <- c("disp","mpg")
lapply(p.vec, function(x) {
p <- ggplot(data = mtcars, aes(x = mpg, y= .data[[x]])) +geom_jitter()
ggsave(sprintf('plot_%s.jpg', x))
})
您也可以使用this answer中的任何选项来代替.data[[x]]
。
https://stackoverflow.com/questions/64334903
复制相似问题