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

E Hofstadler e.hofstadler at gmail.com
Fri Jan 28 12:51:07 CET 2011


Hello Deepayan,

many thanks for your reply and help. Does this solution with
"ylab.right" require a newer version of lattice (somewhere in the
archives I noted "ylab.right" being mentioned in the context of a
development version of lattice)?

I currently use  R version 2.11.1 (2010-05-31) via Emacs (ESS) on a
Windows machine and couldn't quite get your solution to work (the
graph didn't display any text, but neither did R give any error
messages). Perhaps I don't have the right version of the lattice
package?

Regards,
eh





2011/1/28 Deepayan Sarkar <deepayan.sarkar at gmail.com>:
> 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