1

Can we include subscript/superscript characters in rasterVis::levelplot strip labels?

Consider the following RasterStack, s:

library(rasterVis)
s <- stack(replicate(2, raster(matrix(runif(9), 3))))

The default plot method for Raster* objects allows expressions to be passed to the main argument:

plot(s, main=expression(Something, Something[2]))

enter image description here

With rasterVis::levelplot, strip names are passed via the names.attr argument, but it seems these are coerced to character before eventually being passed to lattice::levelplot with strip.custom(factor.levels = names.attr).

The result is:

levelplot(s, names.attr=expression(Something, Something[2]))

enter image description here

Short of modifying the source, is there a way to use expressions (or otherwise achieve subscript/superscript characters) in strip labels of rasterVis::levelplot?

www
  • 38,575
  • 12
  • 48
  • 84
jbaums
  • 27,115
  • 5
  • 79
  • 119
  • 1
    The documentation fpr rasterVis::contourplot does not suggest htat the author expected an exprssion-object. I says "character" for the `names.attr` parameter. And it turns out to be a flipping S4 method, so it will take a lot of digging to figure out what's actually going on. – IRTFM Oct 17 '15 at 04:44

1 Answers1

2

You might think that rasterVis::levelplot was using the code of lattice::levelplot and to an extent that turns out to be the case, but a bunch of data transformation occurs first. The S4-method cannot be seen by using the signature with x="RasterBrick", but rather needs x="Raster".

showMethods("levelplot", classes="RasterStack", includeDefs=TRUE)

getMethod("levelplot", signature=c(x="Raster", data="missing"))

That shows that the code constructing the strips is defined as:

    ....
            strip = strip.custom(factor.levels = names.attr), 
    ....

I would have guessed that factor.levels would have been the correct parameter to pass an expression to. There is a coercion step that is causing the failure. So you need to hack the code to allow an expression to make it through. If I comment out the coercion with as.character:

        else {
         # names.attr <- as.character(names.attr)
         if (length(names.attr) != nlayers(object)) 
             stop("Length of names.attr should match number of layers.")

Using:

setMethod( "levelplot:   {
  function code
  },  signature= c(x="Raster", data="missing"))

And copy two unexported function from rasterVis to the the global environment I get success:

drawMargin <- rasterVis:::drawMargin
constructMargin <- rasterVis:::constructMargin

And pass the uncoerced expressions to 'factor.levels' via names.attr:

png(); print(levelplot(s, names.attr=expression(Something, Something[2])) );dev.off()

enter image description here

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Thanks for taking the time to dig around. I had found the method's source, and the coercion to character, but was hoping there might be a clever way to circumvent it (other than commenting it out). That said, your approach works fine, so I appreciate your post. Maybe @OscarPerpiñán might reconsider the `as.character` ;) – jbaums Oct 17 '15 at 06:35
  • 1
    @jbaums There's a github issue report: https://github.com/oscarperpinan/rastervis/issues –  Oct 17 '15 at 06:38
  • Good point, @Pascal... I was being lazy. Have posted an issue now. – jbaums Oct 17 '15 at 06:43
  • 1
    Fixed now with this [commit](https://github.com/oscarperpinan/rastervis/commit/7d2faf6a77d1962362ee6bca8737ef3c686f9419). Thanks for reporting this issue. – Oscar Perpiñán Oct 17 '15 at 12:50