[R] two histograms in the same graph

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Thu Jan 24 16:10:21 CET 2008


That's an easy one with ggplot2.

library(ggplot2)
dataset <- data.frame(variable = gl(2, 50, labels = c("x", "z")), value
= c(runif(50), rnorm(50)))
ggplot(data = dataset, aes(x = value, fill = variable)) +
geom_histogram(position = "dodge") + scale_fill_manual(values = c("red",
"blue"), guide = "tile") 

But I don't prefer to put two histograms into the same graphs. Making a
seperate histogram for each variable makes is easier to compare them. Or
you could use a density plot.

ggplot(data = dataset, aes(x = value, fill = variable)) +
geom_histogram() + facet_grid(variable ~ .) +  scale_fill_manual(values
= c("red", "blue"), guide = "tile") 
ggplot(data = dataset, aes(x = value, colour = variable)) +
geom_density() + scale_colour_manual(values = c("red", "blue"), guide =
"line") 

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 op inbo.be 
www.inbo.be 

Do not put your faith in what statistics say until you have carefully
considered what they do not say.  ~William W. Watt
A statistical analysis, properly conducted, is a delicate dissection of
uncertainties, a surgery of suppositions. ~M.J.Moroney

-----Oorspronkelijk bericht-----
Van: r-help-bounces op r-project.org [mailto:r-help-bounces op r-project.org]
Namens Juan Pablo Fededa
Verzonden: donderdag 24 januari 2008 15:44
Aan: r-help
Onderwerp: [R] two histograms in the same graph

Dear Contributors:

I have two vectors x and z, and I want to display the histograms of both
vectors in the same graph, x in red bars, z in blue bars.
If you have any clue on how to do that, I will be very glad to hear
it!!!!!!
Thanks in advance again,

Juan Pablo Fededa

	[[alternative HTML version deleted]]

______________________________________________
R-help op 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.



More information about the R-help mailing list