I am trying to create plots within a for loop and output them to a pdf. Here is a working example using plot: gg <- data.frame(datadate=1:4, spread=5:8) pdf() for (i in 1:3) { plot(gg$datadate, gg$spread, main=i) } dev.off() I am trying to learn more about ggplot2 so I try a slight modification and it doesn't work. Anyone know how to do this using qplot in ggplot2? gg <- data.frame(datadate=1:4, spread=5:8) pdf() for (i in 1:3) { qplot(gg$datadate, gg$spread, geom="line", main=i) } dev.off() *************************************************************** This message is for the named person's use only. It may\...{{dropped:20}}
you have to print the object gg <- data.frame(datadate=1:4, spread=5:8)> pdf() > for (i in 1:3) {+ d <- qplot(gg$datadate, gg$spread, geom="line", main=i) + print(d)}> dev.off()On Tue, Mar 23, 2010 at 1:14 PM, Bos, Roger <roger.bos at rothschild.com> wrote:> I am trying to create plots within a for loop and output them to a pdf. > Here is a working example using plot: > > ? ? ? ? ?gg <- data.frame(datadate=1:4, spread=5:8) > ? ? ? ? ?pdf() > ? ? ? ? ?for (i in 1:3) { > ? ? ? ? ? ? ?plot(gg$datadate, gg$spread, main=i) > ? ? ? ? ?} > ? ? ? ? ?dev.off() > > I am trying to learn more about ggplot2 so I try a slight modification > and it doesn't work. ?Anyone know how to do this using qplot in ggplot2? > > > ? ? ? ? ? gg <- data.frame(datadate=1:4, spread=5:8) > ? ? ? ? ?pdf() > ? ? ? ? ?for (i in 1:3) { > ? ? ? ? ? ? ?qplot(gg$datadate, gg$spread, geom="line", main=i) > ? ? ? ? ?} > ? ? ? ? ?dev.off() > > *************************************************************** > > This message is for the named person's use only. It may\...{{dropped:20}} > > ______________________________________________ > R-help at 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. >-- Stephen Sefick Let's not spend our time and resources thinking about things that are so little or so large that all they really do for us is puff us up and make us feel like gods. We are mammals, and have not exhausted the annoying little problems of being mammals. -K. Mullis
Bos, Roger-2 wrote:> > I am trying to create plots within a for loop and output them to a pdf. > Here is a working example using plot: > > gg <- data.frame(datadate=1:4, spread=5:8) > pdf() > for (i in 1:3) { > plot(gg$datadate, gg$spread, main=i) > } > dev.off() > > I am trying to learn more about ggplot2 so I try a slight modification > and it doesn't work. Anyone know how to do this using qplot in ggplot2? > > > gg <- data.frame(datadate=1:4, spread=5:8) > pdf() > for (i in 1:3) { > qplot(gg$datadate, gg$spread, geom="line", main=i) > } > dev.off() >This question gets asked many, many times and is answered in FAQ 7.22: http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-do-lattice_002ftrellis-graphics-not-work_003f Basically, "old-school" functions like plot() execute plotting commands directly when they are called. With Lattice-based graphics, like ggplot2, the composition of the plot and the execution of the plotting commands are separated into two steps. qplot() returns an object that contains the composition of the plot, the print() method performs the actual execution of plotting commands to a graphics device. When functions are called interactively in the top-level environment, an implicit call to print() is executed to display the results. When these same functions are used inside another function or for() loop, you will need to explicitly add the call to print() in order to get the same effect. Hope this helps! -Charlie ----- Charlie Sharpsteen Undergraduate-- Environmental Resources Engineering Humboldt State University -- View this message in context: http://n4.nabble.com/Creating-pdfs-using-qplot-in-qqplot2-tp1679488p1679523.html Sent from the R help mailing list archive at Nabble.com.