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

Peter Ehlers ehlers at ucalgary.ca
Wed Feb 23 03:31:21 CET 2011


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]), ...) })

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!)
> #thanks! really appreciate any input.
>



More information about the R-help mailing list