An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090102/fb977a5f/attachment-0001.pl>
R Stacked Histogram
11 messages · Jason Rupert, Hadley Wickham, John Kane +1 more
On Fri, Jan 2, 2009 at 11:00 PM, Jason Rupert <jasonkrupert at yahoo.com> wrote:
I've seen this asked, but never fully answered. Is it possible to plot stacked histograms in R? I have four data sets that I would like to show combined vertically in histogram format. Is this possible?
Yes, but it's generally not a terribly good way to display your data,
as it is hard to read the values of all except the series on the
bottom.
install.packages("ggplot2")
library(ggplot2)
qplot(carat, data = diamonds, binwidth = 0.1)
qplot(carat, data = diamonds, binwidth = 0.1, fill = cut)
qplot(carat, data = diamonds, binwidth = 0.1, fill = color)
qplot(carat, data = diamonds, binwidth = 0.1, fill = clarity)
More examples at http://had.co.nz/ggplot2/geom_histogram.html
Hadley
Is this of any help? http://www.cse.unsw.edu.au/~mike/myrlibrary/geneplotter/html/histStack.html
--- On Sat, 1/3/09, Jason Rupert <jasonkrupert at yahoo.com> wrote:
From: Jason Rupert <jasonkrupert at yahoo.com>
Subject: [R] R Stacked Histogram
To: r-help at r-project.org
Received: Saturday, January 3, 2009, 12:00 AM
I've seen this asked, but never fully answered.?
Is it possible to plot stacked histograms in R?
I have four data sets that I would like to show combined
vertically in histogram format.?
Is this possible??
Thank you for any feedback you can provide.
P.S. I know I can show the four sets side by side, but I
want to combine them, but still uniquely identify each.??
Thanks again.
[[alternative HTML version deleted]]
______________________________________________ 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.
__________________________________________________________________ Yahoo! Canada Toolbar: Search from anywhere on the web, and bookmark your favourite sites. Download it now at http://ca.toolbar.yahoo.com.
1 day later
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090104/569aa698/attachment-0001.pl>
On Sun, Jan 4, 2009 at 6:51 PM, Jason Rupert <jasonkrupert at yahoo.com> wrote:
Understood. Will head the warning about odd way to display data. Any recommendations about where I look to find full details about "qplot". I tried ?qplot, but it did not return full details. That description was missing a few items, e.g. fill, which is used below.
The best place to start is the qplot chapter of the ggplot2 book - http://had.co.nz/ggplot2/book Regards, Hadley
1 day later
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090106/3df6b06e/attachment-0001.pl>
Dear Jason,
Have a look at scale_y_continuous() and scale_fill_discrete(). This might work (untested as your example is not reproducible with a (dummy) dataset).
qplot(Age, data = recerts_combined_values, binwidth = 5, fill = combined_values$Test.Type, main="Combined Age Histogram") + scale_x_continuous("Age, months") + scale_y_continuous("Counts") + scale_fill_discrete("Type of Tests")
HTH,
Thierry
----------------------------------------------------------------------------
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium
tel. + 32 54/436 185
Thierry.Onkelinx at inbo.be
www.inbo.be
To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data.
~ Roger Brinner
The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey
-----Oorspronkelijk bericht-----
Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Namens Jason Rupert
Verzonden: dinsdag 6 januari 2009 16:00
Aan: r-help at r-project.org
Onderwerp: Re: [R] R Stacked Histogram
Great advice.? I did a quick read and came up with the following:
?
qplot(Age, data = recerts_combined_values, binwidth = 5,
???????? fill = combined_values$Test.Type,
???????? ylab="Counts", xlab="Age, months",?
???????? main="Combined Age Histogram",
???????? legend.name = "Type of Tests")
Unfortunatley, here is a description of the results:
(1) the "main" title font size by default is too large and is clipped by the image (guess I need to figure out how to fix this),
(2) ylab appears to fail - it does not replace the default "count" label - what should I do to fix this?
(3) like (2), neither using "legend.name" nor "legend.title" appears to replace/change the name of the legend title/name.?
?
By any chance can you provide some advice for tackeling these items?? These are probably due to me being a noobie on the ggplot2 package.?
?
Thanks again.
?
--- On Mon, 1/5/09, hadley wickham <h.wickham at gmail.com> wrote:
From: hadley wickham <h.wickham at gmail.com> Subject: Re: [R] R Stacked Histogram To: jasonkrupert at yahoo.com Cc: r-help at r-project.org Date: Monday, January 5, 2009, 7:42 AM On Sun, Jan 4, 2009 at 6:51 PM, Jason Rupert <jasonkrupert at yahoo.com> wrote:
Understood. Will head the warning about odd way to display data. Any recommendations about where I look to find full details about
"qplot".
I tried ?qplot, but it did not return full details. That description was missing a few items, e.g. fill, which is used below.
The best place to start is the qplot chapter of the ggplot2 book - http://had.co.nz/ggplot2/book Regards, Hadley
http://had.co.nz/ [[alternative HTML version deleted]] Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document. The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090107/953ae873/attachment-0001.pl>
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
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090107/45f49e3e/attachment-0001.pl>
1 day later
# 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")
Here's a cute illustration of why I prefer png to jpeg: http://lbrandy.com/blog/2008/10/my-first-and-last-webcomic/ Hadley