[R] Weird but interesting behavior of ggplot2 in a loop

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Sat Jul 19 01:21:41 CEST 2014


FAQ 7.16. ... yes, it applies.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

On July 18, 2014 3:42:14 PM PDT, Dimitri Liakhovitski <dimitri.liakhovitski at gmail.com> wrote:
>Hello and sorry I am not providing an example. However, I hope you'll
>be
>able to answer my question.
>
>I have a "for" loop. Inside this loop I build 4 plots using ggplot2.
>See 2
>blocks of code below - I have in total 4 of those:
>As I step through the loop manually and run all the code inside the
>loop,
>everything works perfectly and R generates (among other things) 4 .png
>files in my working directory (each size 7 KB).
>
>However, when I run the loop (even when the loop length is 1) the
>generated
>.png files are empty (and size 1 KB). I've never seen anything like
>this.
>My loop is correct - because everything else generated in the loop
>except
>for the images is there. And the image files with right names are also
>there, they are just empty.
>
>Any ideas why this might be happening? I did it first in RStudio and
>then
>just in R Gui.
>Thank you very much!
>
>
>
>  png(forname1,width=700,height=450)
>  ggplot(result.opt, aes(x=prices, y=demand))+
>    geom_line(colour='darkblue',size=1.2)+
>
>scale_x_continuous(breaks=seq(input[1,1],input[nrow(input),1],by=step*6))+
>    scale_y_continuous(breaks=seq(0,1,by=0.1),limits=c(0,1))+
>    xlab("Price") + ylab("Purchase Intent")+
>    labs(title=forname2)+
>    theme(plot.title = element_text(size=16))+
>    theme(axis.title.x=element_text(size=12))+
>    theme(axis.title.y=element_text(size=12))
>  dev.off()
>
>  png(forname2,width=700,height=450)
>  ggplot(result.opt, aes(x=prices, y=revenue))+
>    geom_line(colour='blue',size=1.2)+
>
>scale_x_continuous(breaks=seq(input[1,1],input[nrow(input),1],by=step*6))+
>    # scale_y_continuous(breaks=seq(0,1,by=0.1),limits=c(0,1))+
>    xlab("Price") + ylab("Revenue per Person")+
>    labs(title=forname2)+
>    theme(plot.title = element_text(size=16))+
>    theme(axis.title.x=element_text(size=12))+
>    theme(axis.title.y=element_text(size=12))
>  dev.off()



More information about the R-help mailing list