[R] multiple qplot histograms in one plot

Erik Iverson eriki at ccbr.umn.edu
Fri May 21 21:15:02 CEST 2010


> rajesh j wrote:
>> Hi,
>>
>> I wish to plot multiple histograms(representing different data so 
>> different
>> range along xaxis but y axis is the same) horizontally in ggplot2. I'd 
>> like
>> it to look like facets. Is this possible?
> 
> Can you give a small example, say, using the diamonds dataset, or one of 
> your own?  What's wrong with:
> 
>   data(diamonds)
>   qplot(carat, data = diamonds,
>         facets = color ~ .,
>         geom = "histogram",
>         binwidth = 0.1, xlim = c(0,3))
> 
> which is Figure 2.16 in the ggplot2 book.  You can change this to be 
> horizontal.
> 
> Unless you mean different variables completely, in which case you can 
> create your own grid viewports, or possibly use the reshape package 
> along with faceting.  Without a concrete example, it's hard to offer 
> more help.

Here is a complete example of what I think you want, using the reshape 
package.

library(reshape)
library(ggplot2)
tmp <- data.frame(v1 = rnorm(100),
                   v2 = rnorm(100, 10))

melted <- melt(data = tmp, measure.vars = c("v2", "v1"))

qplot(value, data = melted, geom = "histogram") +
     facet_wrap( ~ variable, scales = "free")



More information about the R-help mailing list