[R] adding text to y-axis per row of panels (lattice)
p_connolly at slingshot.co.nz
p_connolly at slingshot.co.nz
Thu Jan 20 22:19:39 CET 2011
On Thu, 20-Jan-2011 at 10:34AM +0200, E Hofstadler 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,...)
|> })
What you want to do would probably require some tricky coding with
grid functions. I think it would be easier, and more effective to
shade the area you want to highlight like this (after you install
latticeExtra:)
install.packages("latticeExtra")
require("latticeExtra")
xyplot(vary ~ varx|condvar, type="o", data = exampledata,
scales = list(alternating = FALSE,
x = list(at=c(1,2,3,4)),
y = list(at=c(-3,-1,0,1,3))),
panel = function(x, y, ...){
panel.xyarea(x = c(0,4.5,4.5,0), y = c(-1, -1, 1, 1),
col = "grey", border = "grey")
panel.xyplot(x,y,...)
}
)
If you prefer, you could shade the too high and too low areas. (You
could even use different colours.
You'll notice that I've extended the polygon to be outside the
plotting range. It has the advantage of avoiding a bug that leaves a
vertical line at the beginning. I've not tried it before, and you
might find a more elegant way of avoiding it.
|>
|>
|>
|> Any help is greatly appreciated!
HTH
--
Patrick Connolly
Plant & Food Research
Mt Albert
Auckland
New Zealand
Ph: +64-9 925 7079
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~
I have the world`s largest collection of seashells. I keep it on all
the beaches of the world ... Perhaps you`ve seen it. ---Steven Wright
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~
More information about the R-help
mailing list