[R] Several densityplots in single figure

David Winsemius dwinsemius at comcast.net
Wed Feb 22 16:49:10 CET 2012


On Feb 22, 2012, at 9:13 AM, josh rosen wrote:

> thank you very much David! Can I follow up with a slight  
> complication of this?
> A 2 by 2 case, where the abline is different in each plot.
>
> A relevant input code would be
>
> thedataA1 <- data.frame(x1=rnorm(100,1,1),x2=rnorm(100,3,1)) #create  
> data
> thedataB1 <- data.frame(x1=rnorm(100,2,1),x2=rnorm(100,4,1)) #create  
> data
> thedataA2 <- data.frame(x1=rnorm(100,1,1),x2=rnorm(100,3,1)) #create  
> data
> thedataB2 <- data.frame(x1=rnorm(100,2,1),x2=rnorm(100,4,1)) #create  
> data
> #four different abline
> v1<-mean(thedataA1$x1)
> v2<-mean(thedataB1$x1)
> v3<-mean(thedataA2$x1)
> v4<-mean(thedataB2$x1)
>
> thedataB1.m<-melt(thedataB1)
> thedataA1.m<-melt(thedataA1)
> thedataA2.m<-melt(thedataA2)
> thedataB2.m<-melt(thedataB2)


Errr ..... You failed to take the lesson that you need to create one  
data.frame with categorical variables. That's a pretty basic lesson  
for both lattice (and ggplot) graphics as well as regression  
applications.
>
> # creating one density plot
> densityplot(~value, thedataA1.m, groups=variable,

Unfortunately there is now no "variable" variable in your new data.

> auto.key=list(columns=2),
> 		panel = function(x, y, ...) {
> 			panel.densityplot(x, ...)
> 			panel.abline(v=mean(thedataA1$x1), lty="dashed", col="red")

After going back and constructing a proper dataset, you should be  
passing 'groups' into the panel function and picking it up inside  
panel.abline. 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



More information about the R-help mailing list