[R] two histograms in the same graph

Domenico Vistocco vistocco at unicas.it
Fri Jan 25 21:04:56 CET 2008


You could use ggplot2:

library(ggplot2)
x=rnorm(100)
y=rnorm(100)
df=melt(data.frame(x,y))
#for "vertical" histogram
ggplot(data=df,aes(x=value))+geom_histogram()+facet_grid(.~variable)
#for "horizontal" histogram  
ggplot(data=df,aes(x=value))+geom_histogram()+facet_grid(.~variable) + 
coord_flip()

Ciao,
domenico

David Daniel wrote:
> You might look at the method histbackback (back-to-back histograms) in  
> package Hmisc.  At the time I looked at this package about a year ago,  
> I could not get it working on my Mac, so I don't actually know what  
> the results look like, but I have created them before in XLispStat and  
> found them useful.  From posts I've seen more recently, I believe that  
> whatever the problem I was encountering previously on the Mac has been  
> resolved.
>
> -David
>
>   
>> 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,
>>     
>
> ----------------------------------
> David Daniel
> Associate Professor
> University Statistics Center
> New Mexico State University
>
> ddaniel at nmsu.edu
>
> ______________________________________________
> 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