[R] How to plot stacked histogram in R?

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Mon May 7 09:03:30 CEST 2012


Have a look at this example

h1 <- cut(rnorm(1000, 4), breaks = seq(0, 8, by = 2))
h2 <- cut(rnorm(1000, 3), breaks = seq(0, 8, by = 2))
h3 <- cut(rnorm(1000, 5), breaks = seq(0, 8, by = 2))
dataset <- data.frame(Set = rep(c("A", "B", "C"), each = 100), value = c(h1, h2, h3))
dataset$fvalue <- factor(dataset$value)
library(ggplot2)
ggplot(dataset, aes(x = value, fill = Set)) + geom_histogram(binwidth = 1) + xlim(1, 5)
ggplot(dataset, aes(x = fvalue, fill = Set)) + geom_histogram()

Best regards,

ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium
+ 32 2 525 02 51
+ 32 54 43 61 85
Thierry.Onkelinx op 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 op r-project.org [mailto:r-help-bounces op r-project.org] Namens Manish Gupta
Verzonden: maandag 7 mei 2012 6:32
Aan: r-help op r-project.org
Onderwerp: Re: [R] How to plot stacked histogram in R?

HI,

Below is third example.

# here we want the full scale from zero to one color.legend(2,6,4,6.4,legend=c("100% guys","100% girls"),
rect.col=color.scale(seq(0,1,by=0.25),c(0.2,1),c(0.2,0.4),c(1,0.4)))
par(mar=c(5,4,4,2))
# use barp to display a multiple histogram
h1<-table(cut(rnorm(100,4),breaks=seq(0,8,by=2)))
h2<-table(cut(rnorm(100,4),breaks=seq(0,8,by=2)))
h3<-table(cut(rnorm(100,4),breaks=seq(0,8,by=2)))
hmat<-matrix(c(h1,h2,h3),nrow=3,byrow=TRUE)
barp(hmat,names.arg=names(h1),width=0.45,col=2:4,
main="Multiple histogram using barp",xlab="Bins",ylab="Frequency")
legend(3.8,50,c("h1","h2","h3"),fill=2:4)


But it does not produce the plot that i need. Can u pls suggest me how to use it to get stacked histogram for two series.

Regards

--
View this message in context: http://r.789695.n4.nabble.com/How-to-plot-stacked-histogram-in-R-tp4602369p4614043.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
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.
* * * * * * * * * * * * * D I S C L A I M E R * * * * * * * * * * * * *
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.



More information about the R-help mailing list