按名称保存绘图列表()
假设我有一个我创建的绘图列表。
library(ggplot2)
plots <- list()
plots$a <- ggplot(cars, aes(speed, dist)) + geom_point()
plots$b <- ggplot(cars, aes(speed)) + geom_histogram()
plots$c <- ggplot(cars, aes(dist)) + geom_histogram()
现在,我想保存所有这些,并用各自的名称(图)元素标记每个元素。
lapply(plots,
function(x) {
ggsave(filename=paste(...,".jpeg",sep=""), plot=x)
dev.off()
}
)
我将用什么替换“...”,以便在我的工作目录中将图保存为:
a.jpeg
b.jpeg
c.jpeg
Let's say I have a list of plots that I've created.
library(ggplot2)
plots <- list()
plots$a <- ggplot(cars, aes(speed, dist)) + geom_point()
plots$b <- ggplot(cars, aes(speed)) + geom_histogram()
plots$c <- ggplot(cars, aes(dist)) + geom_histogram()
Now, I would like to save all of these, labelling each with their respective names(plots) element.
lapply(plots,
function(x) {
ggsave(filename=paste(...,".jpeg",sep=""), plot=x)
dev.off()
}
)
What would I replace "..." with such that in my working directory the plots were saved as:
a.jpeg
b.jpeg
c.jpeg
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能您需要传递列表的名称:
probably you need to pass the names of list:
@kohske 的回答非常精彩!以下是
purrr 0.3.4
版本,适合那些可能喜欢在tidyverse
中工作的人。此外,还会创建一个临时目录来保存绘图,因为 ggsave 默认保存到工作目录。@kohske's answer is sensational! Below is the
purrr 0.3.4
version for those who may prefer working withintidyverse
. Also, a temporary directory is created to hold the plots sinceggsave
defaults to saving to the working directory.