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

Bert Gunter gunter.berton at gene.com
Fri Jul 9 19:44:20 CEST 2010


Don't do this. The overlapping will confuse.

Plot them in a lattice display with one group above the other on the same
horizontal scale. See ?histogram.

Bert Gunter
Genentech Nonclinical Biostatistics
 
 

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Barry Rowlingson
Sent: Friday, July 09, 2010 9:47 AM
To: Mao Jianfeng
Cc: r-help at r-project.org
Subject: Re: [R] how to plot two histograms overlapped in the same
planecoordinate

On Fri, Jul 9, 2010 at 2:29 PM, Mao Jianfeng <jianfeng.mao at gmail.com> 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)
>

 Tried setting xlim with hist?

 par(mfrow=c(2,1))
 hist(age[sex=="M"],xlim=range(age))
 hist(age[sex=="F"],xlim=range(age))

Barry

______________________________________________
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.



More information about the R-help mailing list