Re: [R] two histograms in the same graph

From: hadley wickham <h.wickham_at_gmail.com>
Date: Fri, 25 Jan 2008 14:26:09 -0600

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/

______________________________________________
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.
Received on Fri 25 Jan 2008 - 20:30:25 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 25 Jan 2008 - 21:30:09 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive