[R] How to plot stacked histogram in R?

Jim Lemon jim at bitwrit.com.au
Mon May 7 09:31:29 CEST 2012


On 05/07/2012 02:31 PM, Manish Gupta wrote:
> 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.
>
Ah, I didn't read the question carefully. I would use the "hist" 
function on both series and the plot the result with "barplot".

firstone<-hist(rnorm(100))$counts
secondone<-hist(rnorm(100))$counts
barplot(rbind(firstone,secondone))

Jim



More information about the R-help mailing list