[R] how can I connect paired points within lattice bwplot?

Cory Champagne cory.champagn at gmail.com
Tue Feb 22 20:52:09 CET 2011


Hello all,
my first post to this list.  I do a lot of experiments using a paired 
sampling design and I would get a lot of mileage out of figures like 
this, if I can make it work!  Any advice would be appreciated.
my email is: cory.champagn at gmail.com.
Thanks!


#define dummy variables and a dataframe:
y1 <- c(1:20)
x1 <- c("A","A","A","A","A","A","A","A","A","A", 
"B","B","B","B","B","B","B","B","B","B")
x2 <- c("pre","pre","pre","pre","pre",
"post","post","post","post","post","pre","pre","pre","pre","pre","post","post","post","post","post")
data <- data.frame(y1, x1, x2)


#I'm using the following code to make simple boxplots and it works 
pretty well for me:
with(data, {
boxplot(y1~x1)
points(y1~x1)  #adds the raw data points
for(i in 1:10) { # five individuals in the experiment.
     lines(1:2, c(y1[i], y1[i+10])) } #adds lines connecting the paired 
points, as long as they're ordered correclty anyway.
})  #end boxplot code here.

##Now, I'd like to do the same thing in lattice with multiple factors:
library("lattice")
dev.new()
with(data, {
bwplot(y1~x1|x2,  #make this boxplot with two factors: A & B, and "pre" 
& "post".
     panel=function(...) {
         panel.bwplot(...)
         panel.points(..., pch=16) #up to here- this works well.
         #panel.lines #how do I make this work to add lines to the plot?
     })
})

#I hope this is reasonably clear; I'm trying to add lines connecting the 
paired points in the lattice bwplot.  Any input would be appreciated.  
If anyone's really gung-ho, I'd additionally like to use a separate 
dataframe for the panel.lines function than the panel.points call uses; 
to connect only specific points of interest (but maybe I'm getting over 
my head!)
#thanks! really appreciate any input.

-- 
Cory Champagne, M.Sc.
Long Marine Lab, UC Santa Cruz
office: 831.459.3112
email: cory.champagn at gmail.com



More information about the R-help mailing list