[R] adding text to y-axis per row of panels (lattice)

Deepayan Sarkar deepayan.sarkar at gmail.com
Fri Jan 28 11:46:25 CET 2011


On Thu, Jan 20, 2011 at 2:04 PM, E Hofstadler <e.hofstadler at gmail.com> wrote:
> Dear all,
>
> Being a newbie to R, I've trawled through many old posts on this list
> looking for a solution to my problem, but unfortunately couldn't quite
> figure it out myself. I'd be very grateful if someone here on this
> list could perhaps help me out.
>
> I have a lattice plot with several panels and would like to add some
> text next to the y-axis on the right hand side of each row of panels.
> This text should help the reader to interpret the value ranges of the
> y-axis: so the range between -1 and 1 (in between the two reference
> lines) should e.g. labelled "balanced", the rest of the positive scale
> as "too high", and the rest of the negative scale as "too low". The
> text should be printed in parallel to the y-axis.
>
> If this was a base graphic plot, I'd use mtext, but I'm not sure how
> to get to the same results when using lattice.
>
> Here is some example data:
>
> library(lattice)
> varx <- c(1:4,1:4,1:4,1:4)
> vary <- c(2,2,1.5,0.3,1,2,3,4,-1,-0.5,3,-1,-1,-0.5,1,-3)
> condvar <- c(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4)
> exampledata <-data.frame(cbind(varx,vary,condvar))
> exampledata
> xyplot(vary~varx|condvar, type="o",data=exampledata,
>       scales=list(alternating=F,x=list(at=c(1,2,3,4)),
> y=list(at=c(-3,-1,0,1,3))),
> panel=function(x,y,...){  panel.abline(h=-1)
>                           panel.abline(h=1)
>                           panel.xyplot(x,y,...)
>          })


You could do it like this, but some manual tweaking will be involved
to get nice positions.


labs <- c("too low", "balanced", "too high")
pos <- c(0.14, 0.43, 0.8)

xyplot(vary~varx|condvar, type="o",data=exampledata,
       scales=list(alternating=F,x=list(at=c(1,2,3,4)),
                   y=list(at=c(-3,-1,0,1,3))),
       panel=function(x,y,...){
           panel.abline(h=-1)
           panel.abline(h=1)
           panel.xyplot(x,y,...)
       },
       ylab.right = list(label = rep(labs, 2),
                         y = c(pos/2, 0.52 + pos/2)))


-Deepayan



More information about the R-help mailing list