2

The following code works as designed: a png file is being generated with the desired result.

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() 

However, if I put this in a for loop, only empty png files are being generated. The for loop I use:

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()     
}

The dataframe that I am using (w) is not that important I think, because the problem is with the for loop. Also the for loop iterating only once, but still it is not working.

I have searched SO for the problem, but I have only found solutions for printing the ggplot and not generating png file out of it. So the solutions there (print()) are not good for me. Please help me out how to generate png files from ggplot within a for loop?

Aniko Nagy
  • 35
  • 2
  • 6
    You need to `print` the plot explicitly (or better yet, use `ggsave`). – Gregor Thomas Apr 19 '16 at 21:11
  • @Gregor Why is this a duplicate? The OP tries to save the ggplot into a png file within the loop, while the question and answer you have marked as dup has nothing to do with png. That is simply to generate the plot, not to save it to png. – user2448122 Apr 20 '16 at 06:47
  • Gregor, thank you for your comment. Print() is not an option because it does not generate png file for me. I tried with ggsave, with out any success. – Aniko Nagy Apr 20 '16 at 07:11
  • You need to print your ggplot to your graphical device. Check the linked answer by Gregor. – Adam Quek Apr 20 '16 at 07:49
  • @user2448122 It's a dupe because the `png` doesn't matter. The problem is the same (ggplot works outside of a loop, doesn't work inside a loop) and the solution is the same (explicit use of `print`). The graphic device doesn't matter, be it a file such as a `png` or `pdf` or the device window for the R session. – Gregor Thomas Apr 20 '16 at 15:53
  • Moreover, if you click on the duplicate link, you'll see that *it's* a duplicate too, of a question which is also a duplicate! This is an incredibly common question, the second level dupe links to [Official R-FAQ 7.22](https://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-do-lattice_002ftrellis-graphics-not-work_003f), which deals explicitly with this issue. I chose the dupe I did because of how nicely concise the answer is. They're all linked together now (including this question), providing a much more helpful resource together than they would separately. – Gregor Thomas Apr 20 '16 at 15:57
  • @Gregor finally i was able to solve this problem with the help of ggsave(). Thanks for your input! – Aniko Nagy Apr 20 '16 at 18:47

0 Answers0