[R] Creating pdfs using qplot in qqplot2

Sharpie chuck at sharpsteen.net
Tue Mar 23 19:53:42 CET 2010



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.



More information about the R-help mailing list