Search code examples
rplotlegendlatticelevelplot

Add sp.points key to levelplot colorkey


Is it possible to add the key for, e.g., an sp.points layer, to the colorkey generated by levelplot?

Take the following example:

library(rasterVis)
library(latticeExtra)
library(sp)

r <- as.factor(raster(matrix(rbinom(100, 1, 0.5), 10)))
levels(r)[[1]] <- data.frame(ID=0:1, z=c('a', 'b'))
p <- SpatialPoints(matrix(runif(20), 10))

levelplot(r, margin=list(draw=FALSE), scales=list(draw=FALSE),
          col.regions=c('white', 'gray90')) +
  latticeExtra::layer(sp.points(p, pch=20, col=1))

enter image description here

I would like to add a key entry for the points, below the existing colorkey.

A kludgy solution is to add a key to the levelplot call as follows, adjusting the x and y values until it's in the desired location, but (1) finding the right x and y values is a pain, requiring interaction, (2) the right padding doesn't resize to accommodate the key, and (3) the font size is not automatically scaled to be consistent with the colorkey.

k <- list(x = 1.02, y = 0.4, corner = c(0, 0), points=list(pch=20, col=1), 
          text=list('foo', cex=0.9))

levelplot(r, margin=list(draw=FALSE), scales=list(draw=FALSE),
          col.regions=c('white', 'gray90'), key=k) +
  latticeExtra::layer(sp.points(p, pch=20, col=1))

enter image description here

Assuming I need to stick with lattice graphics, what's the best way to overcome the issues I listed above?


Solution

  • Although it does not solve all the issues you raised, maybe the latticeExtra::mergedTrellisLegendGrob function is useful for you:

    p1 <- levelplot(r, scales=list(draw=FALSE),
                    col.regions=c('white', 'gray90'))
    
    myPoints <- SpatialPoints(matrix(runif(20), 10))
    p2 <- spplot(myPoints, pch=20)
    
    ## Merge graphics
    p <- p1 + p2
    
    ## Merge legends
    l1 <- p1$legend$right
    l2 <- p2$legend$bottom
    ll <- mergedTrellisLegendGrob(l1, l2)
    p$legend$right$fun <- ll
    
    p
    

    mergedLegends