如何在 R 中使用 for 循环生成多个图

How to generate multiple graphs with for loop in R

我想用 for 循环生成一系列图表。对于每个图表,我都有一个 csv 文件和不同的标题。每个图形都应保存为 PNG 和 SVG。每个图形都将以不同的样式(=主题)生成。

所有csv文件都有三列(年份、类型、值),图表是一个折线图,X轴是年份,Y轴是值。每个 "type" 在图表上形成一条线。

我开始为单个图表创建脚本:

library(ggplot2)
library(ggthemes)

filename = "firstgraph"
filename_csv = paste0(filename,".csv")
titlestring = "Title of [firstgraph]"
labelstrings = c("\"label1\"", "\"label2\"", "\"label3\"")

my.data <- read.csv(file=filename_csv, sep = ";", header = TRUE)

# Plot
my.data %>%
  ggplot( aes(x=year, y=value, group=type, color=type)) +
  geom_line(size=1.5) +
  labs(title = titlestring, 
       subtitle = "static subtitle", 
       x = "Jahr", 
       y = "%", 
       caption = "static caption") +
  scale_x_continuous(breaks=seq(1800,2015,10)) +
  scale_y_continuous(breaks=seq(0,100,10)) +
  theme_economist() + scale_color_economist(labels = labelstrings, 
                                            name = "Variante") -> plot_1

plot_1

svg(filename=paste0("plot_",filename,".svg"), 
    width=16, 
    height=7, 
    pointsize=12)
plot_1
dev.off()

png(filename=paste0("plot_",filename,".png"), 
    type = "cairo",
    width=2000, 
    height=800, 
    pointsize=16,
    res=96)
plot_1
dev.off()

这工作正常并生成所述图形的 SVG 和 PNG 版本。 现在我想对一系列图表执行此操作并使用 for 循环扩展脚本。

library(ggplot2)
library(ggthemes)

styles <- c("1", "2")
filenames <- c("firstgraph", "secondgraph")
labelstring_list <- matrix(list(), nrow=2, ncol=1)
labelstring_list[[1,1]] <- c("\"label1\"", "\"label2\"", "\"label3\"")
labelstring_list[[2,1]] <- c("\"label2_1\"", "\"label2_2\"", "\"label2_3\"")

i = as.integer(1)
for(mname in filenames) 
  {
  filename_csv = paste0(mname,".csv")
  titlestring = paste0("Entwicklung des Gebrauchs von [", mname, "]")
  labelstrings = labelstring_list[i]

  my.data <- read.csv(file=filename_csv, sep = ";", header = TRUE)

  my.data %>%
    ggplot( aes(x=year, y=value, group=type, color=type)) +
    geom_line(size=1.5) +
    labs(title = titlestring, 
         subtitle = "static subtitle", 
         x = "Jahr", 
         y = "%", 
         caption = "static subtitle") +
    scale_x_continuous(breaks=seq(1800,2015,10)) +
    scale_y_continuous(breaks=seq(0,100,10)) -> plot_1

  for(style in styles)
    {
    if(style=="1") 
      {
      plot_1 +
        theme_economist() + scale_color_economist(labels = labelstrings, 
                                                  name = "Variante") -> plot_x
      }
    if(style=="2") 
      {
      plot_1 +
        theme_wsj()+ scale_colour_wsj("colors6",labels = labelstrings, 
                                      name = "Variante") -> plot_x
      }

    svg(filename=paste0("plot_",mname,"_",style,".svg"), 
        width=16, 
        height=7, 
        pointsize=12)
      plot_x
    dev.off()

    png(filename=paste0("plot_",mname,"_",style,".png"), 
        type = "cairo",
        width=2000, 
        height=800, 
        pointsize=16,
        res=96)
      plot_x
    dev.off()

    }

  i = (i+1)
  }

这不再有效了。我在控制台中没有错误,并且创建了以下文件:

这些文件都是223字节,都是空的。未创建预期的 PNG 文件。

我检查了 print() 变量中的名称等是否正确并且一切正常:

  print(mname)
  print(filename_csv)
  print(titlestring)
  print(labelstrings)

这输出:

[1] "firstgraph"
[1] "firstgraph.csv"
[1] "Entwicklung des Gebrauchs von [firstgraph]"
[[1]]
[1] "\"label1\""   "\"label2\"" "\"label3\"" 

[1] "secondgraph"
[1] "secondgraph.csv"
[1] "Entwicklung des Gebrauchs von [secondgraph]"
[[1]]
[1] "\"label2_1\""   "\"label2_2\"" "\"label2_3\"" 

这对我来说似乎没问题,但正如我所说,PNG 未创建且 SVG 为空。我尝试只生成 PNG(没有 SVG),但没有用。

谁能告诉我哪里出错了?

非常感谢。

你能试试替换一下吗

png(filename=paste0("plot_",mname,"_",style,".png"), 
    type = "cairo",
    width=2000, 
    height=800, 
    pointsize=16,
    res=96)
  plot_x
dev.off()

来自

plot_x
mirror <- recordPlot()
png(filename=paste0("plot_",mname,"_",style,".png"), 
    type = "cairo",
    width=2000, 
    height=800, 
    pointsize=16,
    res=96)
replayPlot(mirror)
dev.off()

在你的循环中重新测试?