[R] Custom axis function in lattice:::xyplot

Peter Ehlers ehlers at ucalgary.ca
Wed May 26 19:20:24 CEST 2010


On 2010-05-26 4:03, Gavin Simpson wrote:
> 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))

# There must a better way, but this works; add

   labels = levels(dat$fac)[panel.number()],

# to your panel.axis() call in axis.VarLabs.

> 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.

# Look at the settings for layout.heights, especially
# axis.top and top.padding:

  xyplot(....,
     par.settings = list(layout.heights =
         list(axis.top = xxx,
              top.padding = xxx)))

# (Shouldn't need both; I don't know which is more
# suitable for you.)

   -Peter Ehlers



More information about the R-help mailing list