Inside a loop or other function you need an explicit print statement. http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-do-lattice_002ftrellis-graphics-not-work_003f
Sarah On Friday, July 18, 2014, Dimitri Liakhovitski < dimitri.liakhovit...@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() > > > -- > Dimitri Liakhovitski > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org <javascript:;> mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > -- Sarah Goslee http://www.stringpage.com http://www.sarahgoslee.com http://www.functionaldiversity.org [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.