r - 在ggplot2中循环ID,然后单独保存每个图
问题描述
我有一个长格式的大型数据框,其中包含大约 13k 行。这是它的外观示例数据。
# make data
set.seed(1234)
id <- c(101,101,101,101,101,101,101,101,101,
102,102,102,102,102,102,
103,103,103,103,103,103,103,103,103,103,103)
time <- c(1:9, 1:6, 1:11)
var1 <- sample(1:20, 26, replace=TRUE)
df <- data.frame(id,time,var1)
我想要:
- 为每个 id 生成 x=time 和 y=var1 的图列表
- 将每个 id 的图单独保存为文件夹的图像
到目前为止,我的代码是:
# loop to make plots
library(tidyverse)
id <- unique(df$id)
for(i in id){
list <- ggplot(df, aes_string(x = time, y = var1)) +
geom_point() +
stat_smooth() +
ggtitle("Plot for", paste(df$id))
print(list)
}
# loop to save plots
filename <- paste("plot_", df$id, ".png")
path <- "~/test"
for(i in list){
ggsave(filename = filename, plot = plot[[i]], path = path)
}
制作ggplots列表的代码运行没有错误,但是每当我尝试查看列表时,它只显示id 101的绘图。代码保存绘图列表导致错误:Error: device must be NULL, a string or a function.
应如何修复代码以实现两个目标?
解决方案
您的脚本在循环的每次迭代中都会覆盖列表。创建绘图时保存绘图会更容易。
但是,如果您想存储在每次迭代中创建的绘图以供稍后在脚本中使用,我建议在循环之外创建一个列表对象,然后在每次迭代中附加对象。
有关更多详细信息,请参阅评论。
# Create an empty list
plotlist <- list()
id <- unique(df$id)
for(i in id){
#filter the dataframe for each id
list <- df %>% filter(id==i) %>% ggplot(aes(x = time, y = var1)) +
geom_point() +
stat_smooth() +
ggtitle("Plot for", paste(i))
#print the plot
print(list)
#store the plot in the list with id as the name
plotlist[[as.character(i)]] <- id
}
现在可以列出这个列表:“plotlist”,以便稍后进行额外分析。
推荐阅读
- c# - 从特定点而不是中心旋转图像c#
- python - 如何忽略python中的打印输出?
- foursquare - 在 apifoursquare 上发送请求时出现 403 响应代码
- xml - 使用 XSLT 对 XML 中的一个元素进行分组
- java - 如何将二叉树中的节点推送到数组中?
- javascript - Moment.js HTTP 服务器结果当前未正确输出
- python - 如何在使用 BeautifulSoup 解析页面时仅获取有效的 url 链接?
- websocket - 模式匹配 gremlin 查询的结果只有在有 91 个匹配查询时才返回 64 个顶点
- reactjs - React Semantic UI - 如何禁用基于父组件的子组件
- java - if 语句的“立即正常完成”是什么意思?