# [R] Lattice and horizontally stacked density plots

Manish Nag nagm01 at gmail.com
Thu Feb 23 05:28:17 CET 2012

```> With lattice alone you can easily get all curves on the same level:
> densityplot(~ val | factor(id2), groups=factor(id1),data=a_df,pch='|')
>

I just tried the method above. Unfortunately it just makes plots with
different colored lines that overlap one another. Can anyone point me
to an example where people plot their own lines in a panel function?

-Manish

> But if that doesn't do it for you, you could write your own panel
> function. I don't have time to try it but I'm thinking one of these
> might work
> 1)
> create new ylim[1] from current.panel.limits()\$ylim / number of groups
> dens<- density(x)
> use lines and polygons to draw the curves dens\$x,dens\$y at each new ylim level.
>
> 2) grid may come in handy here, splitting each panel into several viewports ?
>
> 3) use bwplot for "setup" but plot polygons of density (steps 2:3 from option 1)
>
> Good luck with that.
>
> Elai
>
>
>
> On Wed, Feb 22, 2012 at 3:01 PM, Manish Nag <nagm01 at gmail.com> wrote:
>> Hello,
>>
>> I am try to make a density plot where plots are stacked like the one
>> found here:
>>
>> http://dsarkar.fhcrc.org/lattice/book/images/Figure_14_03_stdBW.png
>>
>> I am facing problems, however. Using the code example below, I'd like
>> to generate a separate panel for each val of id2. Within each panel,
>> I'd like to have individual histograms each on separate lines based on
>> the value of id1.  Note that the code example works fine if I use
>> "boxplot" instead of "densityplot". Any pointers would be much
>> appreciated.
>>
>>
>> library(lattice)
>> val<-rep(rnorm(10),100)
>> id1<-sample(c(1:5), 100, replace = TRUE)
>> id2<-rep(c(6:10),100, replace = TRUE)
>> a_df<-data.frame(cbind(id1, id2, val))
>> densityplot(factor(id1) ~ val | factor(id2), data=a_df)
>>
>> -Manish
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help