以下代码按设计工作:正在生成具有所需结果的png文件。
rr <- createPlot(w[1, "hoofdstuk.nr"], w[1, "paragraaf.nr"], w[1, "paragraaf.titel"], i )
mu <- ddply(rr, "school", summarise, grp.mean=mean(minute))
mypath <- file.path("D:", "work","sf-datascience","avgtimespent", "newplots",paste0("plot", w$hoofdstuk.nr, "-", w$paragraaf.nr, ".png"))
png(file=mypath)
png(paste0("plot", w$hoofdstuk.nr[i], "-", w$paragraaf.nr[i], ".png"), width=800, height=600)
ggplot(rr, aes(x = minute, color = school)) + geom_density() + geom_vline(data=mu, aes(xintercept=grp.mean, color=school),
linetype="dashed") + labs(title=rr[i, "paragraaf.titel"],x="Minute", y = "Density")
dev.off()
但是,如果我将它放在for循环中,则只生成空的png文件。我使用的for循环:
for (i in 1:1) {
rr <- createPlot(w[1, "hoofdstuk.nr"], w[1, "paragraaf.nr"], w[1, "paragraaf.titel"], i )
mu <- ddply(rr, "school", summarise, grp.mean=mean(minute))
mypath <- file.path("D:", "work","sf-datascience","avgtimespent", "newplots",paste0("plot", w$hoofdstuk.nr, "-", w$paragraaf.nr, ".png"))
png(file=mypath)
png(paste0("plot", w$hoofdstuk.nr[i], "-", w$paragraaf.nr[i], ".png"), width=800, height=600)
ggplot(rr, aes(x = minute, color = school)) + geom_density() + geom_vline(data=mu, aes(xintercept=grp.mean, color=school),
linetype="dashed") + labs(title=rr[i, "paragraaf.titel"],x="Minute", y = "Density")
dev.off()
}
我认为我使用的数据帧(w)并不重要,因为问题在于for循环。 for循环也只迭代一次,但仍无法正常工作。
我搜索了SO的问题,但我只找到了打印ggplot的解决方案,而没有生成png文件。所以那里的解决方案(print())对我不好。 请帮我解释如何在for循环中从ggplot生成png文件?