我想生成一系列带有for循环的图形。对于每个图,我都有一个csv文件和不同的标题。每个图应另存为PNG和SVG。每个图将以不同的样式(=主题)生成。
所有csv文件都有三列(年,类型,值),并且该图是一个折线图,其中年份为X,Y值为轴。每个“类型”都会在图形上显示一条线。
我开始为单个图形创建脚本:
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)。
有人可以告诉我我在哪里犯错吗?
非常感谢。
答案 0 :(得分:1)
您可以尝试替换
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()
在您的循环中并重新测试?