首页 > 解决方案 > 在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)

我想要:

  1. 为每个 id 生成 x=time 和 y=var1 的图列表
  2. 将每个 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.应如何修复代码以实现两个目标?

标签: rloopsggplot2plot

解决方案


您的脚本在循环的每次迭代中都会覆盖列表。创建绘图时保存绘图会更容易。

但是,如果您想存储在每次迭代中创建的绘图以供稍后在脚本中使用,我建议在循环之外创建一个列表对象,然后在每次迭代中附加对象。
有关更多详细信息,请参阅评论。

# 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”,以便稍后进行额外分析。


推荐阅读