[R] how to plot two histograms overlapped in the same plane coordinate

Jim Lemon jim at bitwrit.com.au
Sat Jul 10 10:20:00 CEST 2010


On 07/09/2010 11:29 PM, Mao Jianfeng wrote:
> Dear R-help listers,
>
> I am new. I just want to get helps on how to plot two histograms
> overlapped in the same plane coordinate. What I did is very ugly.
> Could you please help me to improve it? I want to got a plot with semi-
> transparent overlapping region. And, I want to know how to specify the
> filled colors of the different histograms.
>
> I also prefer other solutions other than ggplot2.
>
> Many thanks to you.
>
>
> What I have done:
>
> library(ggplot2)
>
> age<-c(rnorm(100, 1.5, 1), rnorm(100, 5, 1))
> sex<-c(rep("F",100), rep("M", 100))
> mydata<-cbind(age, sex)
> mydata<-as.data.frame(mydata)
> head(mydata)
>
>
> qplot(age, data=mydata, geom="histogram", fill=sex, xlab="age",
> ylab="count", alpha=I(0.5))
>
Hi Mao,
Have a look at the last example on the help page of the "barp" function 
in plotrix. This shows how to plot a multiple histogram similar to what 
you have done.

Jim



More information about the R-help mailing list