[R] two histograms in the same graph

hadley wickham h.wickham at gmail.com
Fri Jan 25 21:26:09 CET 2008


On Jan 25, 2008 2:04 PM, Domenico Vistocco <vistocco at unicas.it> wrote:
> 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()

And it's moderately easy to create a back-to-back histogram too:

qplot(x, geom="histogram", fill="x", binwidth=0.5) +
geom_histogram(data=data.frame(x=y), aes(fill="y", y=-..count..),
binwidth= 0.5) +
scale_fill_hue("variable")

Hadley

-- 
http://had.co.nz/



More information about the R-help mailing list