Dan Bolser
2007-Apr-04 11:19 UTC
[R] Annotate a levelplot (using abline) - Difficulty with trellis.
Hi, I am generating a beautiful plot with the 'levelplot' function over my square matrix of data. In order to help visualise the data I would like to draw a diagonal line on the matrix. Because the plot is actually a trellis object, I am having difficulty working out how to do this. I have been reading around, but I don't see any easy solution to the problem. (Most of the docs I have found are not of the type 'how to do it' but more like 'how to grok it'). After spending 1+ hour reading and trying various things I figure its time to ask some people who know ;-) So far I have the following (which almost works!)... levelplot( our.data, plot.xy = (abline(0,1,col="white")) ) However the coordinate system / plot area being used are clearly not those of the square matrix. I guess I should point out that the axes of 'our.data' (the row and column names of the square matrix) are ordered categories of the form; seq(2,9,0.5) Thanks for any help ! Dan.
Charilaos Skiadas
2007-Apr-04 12:25 UTC
[R] Annotate a levelplot (using abline) - Difficulty with trellis.
On Apr 4, 2007, at 7:19 AM, Dan Bolser wrote:> Hi, > > I am generating a beautiful plot with the 'levelplot' function over my > square matrix of data. In order to help visualise the data I would > like to draw a diagonal line on the matrix. Because the plot is > actually a trellis object, I am having difficulty working out how to > do this. I have been reading around, but I don't see any easy solution > to the problem. (Most of the docs I have found are not of the type > 'how to do it' but more like 'how to grok it'). After spending 1+ hour > reading and trying various things I figure its time to ask some people > who know ;-) > > So far I have the following (which almost works!)... > > levelplot( > our.data, > plot.xy = (abline(0,1,col="white")) > )1) Please always provide a reproducible example. 2) Normal drawing commands, like abline, can't be used in trellis graphics, and vice versa. 3) Look into ?panel.functions, in particular panel.abline. My understanding is that this is how you customize a graph, providing your own panel function which calls other panel functions or direct grid drawing commands. Hope this helps.> However the coordinate system / plot area being used are clearly not > those of the square matrix. I guess I should point out that the axes > of 'our.data' (the row and column names of the square matrix) are > ordered categories of the form; > > seq(2,9,0.5) > > > Thanks for any help ! > > Dan.Haris Skiadas Department of Mathematics and Computer Science Hanover College
Dan Bolser
2007-Apr-04 14:52 UTC
[R] Annotate a levelplot (using abline) - Difficulty with trellis.
My question was thus; Given library(lattice) my.m <- matrix(seq(1,100,1),nrow=10) levelplot(my.m) How can I add a diagonal line onto the resulting 'color square'? The answer I found was to hack the 'panel.levelplot' function. Here is the diff between the old (panel.levplot) and the new (my.panel.levplot) functions; *************** *** 72,77 **** --- 82,89 ---- shrinky[2], fullZrange), default.units = "native", gp = gpar(fill = col.regions[zcol], lwd = 1e-05, col = "transparent", alpha = alpha.regions)) + ## Added by Dan + panel.abline(0,1,col="white") if (contour) { if (is.logical(labels) && !labels) labels <- NULL And then use... library(lattice) my.m <- matrix(seq(1,100,1),nrow=10) levelplot(my.m, panel=my.panel.levelplot) I accidentally started to talk to Charilaos Skiadas 'off-list', so thanks to him for help. Dan. [[alternative HTML version deleted]]