[R] Custom axis function in lattice:::xyplot
Gavin Simpson
gavin.simpson at ucl.ac.uk
Wed May 26 12:03:16 CEST 2010
Dear List,
I have been writing a Lattice function to draw what we call
stratigraphic diagrams, these are diagrams with a panel for each species
showing a time series of abundance, but drawn vertically to represent
time passing from bottom of plot towards to top.
I am most of the way there with this now, but I want to do away with the
strip on each panel and instead draw a custom top axis with a single
tick mark (situated at the panel midpoint) with label equal to that
which would be used for the strip text. I can do all of this except get
the correct label. How do I get access to the factor level being plotted
in each panel within my custom axis function?
It may be easier to illustrate than explain, so here is a dummy example,
including a version of my custom axis function.
require(lattice)
## custom axis function
axis.VarLabs <- function(side, ...) {
if(isTRUE(all.equal(side, "top"))) {
M <- function(lims) min(lims) + (diff(lims) / 2)
xlim <- current.panel.limits()$xlim
panel.axis(side = side, outside = TRUE, at = M(xlim),
tck = 1, line.col = "black",
text.col = "black", rot = 45)
} else {
axis.default(side = side, ...)
}
}
## demo data
set.seed(123)
dat <- data.frame(X = rnorm(1000), Y = rpois(1000, 2),
fac = sample(gl(4, 500, labels = LETTERS[1:4])))
head(dat)
str(dat)
## xyplot with custom axis --- note I leave the strip on here to show
## that I want the labels to be A, B, C, D on the top axis ticks...
xyplot(Y ~ X | fac, data = dat, axis = axis.VarLabs, layout = c(4,1,1))
Notice how in this plot, because I have not specified a label the 'at'
value for this point is used. Is there a way to get at the factor levels
from within an axis function? If so, how?
Secondly, how do you ensure there is enough space around the plot to
draw the axis labels without truncation - as can be seen on the example
xyplot.
Thanks in advance,
Gavin
--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Dr. Gavin Simpson [t] +44 (0)20 7679 0522
ECRC, UCL Geography, [f] +44 (0)20 7679 0565
Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk
Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/
UK. WC1E 6BT. [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
More information about the R-help
mailing list