[R] Several densityplots in single figure

ilai keren at math.montana.edu
Wed Feb 22 19:21:20 CET 2012


On Wed, Feb 22, 2012 at 8:49 AM, David Winsemius <dwinsemius at comcast.net> wrote:

>
> After going back and constructing a proper dataset, you should be passing
> 'groups' into the panel function and picking it up inside panel.abline.

Close, but unfortunately things get more complicated when using groups
in densityplot. A straight up panel function wouldn't work for him out
of the box (and you/list be getting more follow ups). He'll need
something like:
densityplot(~value | g1, groups=g2, data= combinedlongformat,
 panel=panel.superpose, panel.groups=function(x,...,group.number){
 panel.abline(v=mean(x),lwd=(1:0)[group.number])  # seems like he only wants x1
 panel.densityplot(x,...)
 })

At this stage in the game
trellis.focus('panel',1,1) ; panel.abline(v=calculatedmean1)
trellis.focus('panel',2,1) ; panel.abline(v=calculatedmean2)
...
trellis.unfocus()

Would probably go down easier...

Just a thought

Cheers,
Elai


You
> can also recover the 'panel.number()' and then use them both to recover the
> means from the global environment when inside panel.abline. I probably would
> have used tapply to create a table with an index (in the enclosing
> enviroment) rather than creating 4 separate values to choose That way you
> could use one object name but construct the 4 indices. But for that to work
> you would have needed to follow my example about binding the 4 datasets into
> one.
>
>
> One way to find previous worked examples in the rhelp Archives is to search
> with your favorite engine on strategies like "panel.number groups Sarkar" or
> "panel.number groups Andrews"
>
> Deepayan Sarkar and Felix Andrews are the two persons from whom I have
> learned the most regarding the fine points of lattice plots.
>
> And will you please learn to post in plain text?
>
> --
> david
>
>
>>                }
>> )
>>
>>
>>
>>
>>
>>
>> On 22 February 2012 13:48, David Winsemius <dwinsemius at comcast.net> wrote:
>>
>> On Feb 22, 2012, at 5:28 AM, josh rosen wrote:
>>
>> Hi,
>>
>> I have created two separate overlapping density plots- see example code
>> below.
>> What I wish now to do is combine them into one figure where they sit side
>> by side.
>> Any help would be great!
>>
>> many thanks in advance, josh.
>>
>> #####################
>> thedataA <- data.frame(x1=rnorm(100,1,1),x2=rnorm(100,3,1)) #create data
>> thedataA.m<-melt(thedataA)
>>
>> densityplot(~value, thedataA.m, groups=variable,auto.key=list(columns=2),
>>
>>     panel = function(x, y, ...) {
>>             panel.densityplot(x, ...)
>>             panel.abline(v=0)
>>     }
>> )
>>
>>
>> The syntax for grouping (which gives theoverlaid but different colors as
>> default output) and "separation" is fairly simple. Use the "|" operator for
>> separated plots and the " .., groups= <var>, .."  parameter for overlaying
>> results. It's only going to work easily if they are all in the same dataset.
>>
>> Try:
>>
>>  bigset <- cbind( rbind(thedataA.m, thedataB.m), ABgrp=rep(c("datA",
>> "datB"), each=200) )
>>  densityplot(~value|ABgrp, data=bigset, groups=variable,
>> auto.key=list(columns=2),
>>
>>      panel = function(x, y, ...) {
>>              panel.densityplot(x, ...)
>>              panel.abline(v=0)   } )
>>
>>
>> And please work on whatever practice is producing duplicate postings.
>>
>> --
>> David.
>>
>>
>>
>>
>> thedataB <- data.frame(x1=rnorm(100,2,1),x2=rnorm(100,4,1)) #create data
>>
>> thedataB.m<-melt(thedataA)
>>
>> I assume that is a copy-paste-fail-to-correct error.
>>
>>
>> densityplot(~value, thedataB.m, groups=variable,auto.key=list(columns=2),
>>
>>     panel = function(x, y, ...) {
>>             panel.densityplot(x, ...)
>>             panel.abline(v=0)
>>     }
>> )
>> ######################
>>
>>       [[alternative HTML version deleted]]
>
>
>
> David Winsemius, MD
> West Hartford, CT
>
> ______________________________________________
> 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