[R] R Stacked Histogram

hadley wickham h.wickham at gmail.com
Wed Jan 7 16:20:04 CET 2009


On Wed, Jan 7, 2009 at 7:55 AM, Jason Rupert <jasonkrupert at yahoo.com> wrote:
> That worked!  Thank you very much for the help.
>
> I think I have one last question about qplot in ggplot2, That is exporting the plot to JPG while in a loop.   I tried what was recommended in "Re: [R] jpeg() creating empty files with qplot() in a loop", however, I received some errors (see below).  I also tried using a PDF, but that did not work either, i.e. the PDF produced was blank.  Oddly enough the results produced using "dev.print(file="qplot_output.jpg", device=jpeg, width=600) " in a loop are blank, i.e. the JPG file is there, but it is blank.  Unfortunately,  "ggsave(file="ggsave_qplot_output.jpg")" does not even produce a JPG file output.  Any help at this point is greatly appreciated.  Is the problem related to the fact that I need to use the plain "hist" and "dev.print" prior to using ggplot's qplot?

If you're using ggsave, you don't need to use the dev functions.  The
following should be sufficient:

qplot(rnorm_test_data, geom = "histogram", binwidth = 1) +
         scale_x_continuous("Test Data") + scale_y_continuous("Frequency") +
         scale_fill_discrete("qplot histogram")
ggsave(file = "ggsave_qplot_output.pdf")

# Unfortunately due to a small bug in the current version of ggplot,
you need to use
# jpeg with an e to get jpegs
ggsave(file = "ggsave_qplot_output.jpeg")

# But I'd recommend using png instead - much better quality
ggsave(file = "ggsave_qplot_output.png")

# One final problem is that you're going to overwrite the plot
# on each run of your loop.  You probably want something like:

ggsave(file = paste("ggsave_qplot_output_", i, ".png", sep =""))

Hadley

-- 
http://had.co.nz/




More information about the R-help mailing list