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

Deepayan Sarkar deepayan.sarkar at gmail.com
Wed Feb 23 05:16:04 CET 2011


On Wed, Feb 23, 2011 at 8:01 AM, Peter Ehlers <ehlers at ucalgary.ca> wrote:
> On 2011-02-22 11:52, Cory Champagne wrote:
>>
>> 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?
>>      })
>> })
>
> See if this works for you:
> [I've changed your 'data' to 'dat' and you don't need the with()]
>
>  bwplot(y1 ~ x1 | x2, data = dat,
>     panel=function(x, y, ...) {
>       panel.bwplot(x, y, ...)
>       panel.points(x, y, ..., pch=16, col=2)
>       for(i in 1:5) panel.lines(1:2, c(y[i], y[i+5]), ...) })

Another version with some error-checking:

bwplot(y1 ~ x1 | x2, data = dat,
       panel=function(x, y, ...) {
           panel.bwplot(x, y, ...)
           panel.points(x, y, ..., pch=16, col=2)
           ys <- split(y, x)
           n <- unique(sapply(ys, length))
           stopifnot((length(ys) == 2) &&
                     (length(n) == 1))
           panel.segments(rep(1, n), ys[[1]],
                          rep(2, n), ys[[2]], ...)
       })

>
> Peter Ehlers
>
>>
>> #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!)

That would actually make more sense, as in general bwplot() does not
require the number of points to be same for all groups. A skeleton of
the approach could be:

bwplot(y1 ~ x1 | x2, data = dat, extra.data = dat2,
       panel=function(x, y, ..., extra.data) {
           panel.bwplot(x, y, ...)
           panel.points(x, y, ..., pch=16, col=2)
           cp <- packet.number()
           extra.data.sub <- some.function.of(extra.data, cp)
           panel.segments(<...>)
       })

The key points being that

1. extra arguments can be passed as-is to the panel function
('extra.data' here), and

2. you would probably want to use packet.number() to figure out which
subset you are plotting and extract the relevant part of 'extra.data'.

-Deepayan



More information about the R-help mailing list