Search code examples
rlatticelevelplotcolor-key

levelplot: how to add space between colorkey and x-axis label


I am trying to use levelplot to plot a simple Digital Elevation Model (DEM).

Here is my code:

r1 = raster("ned10dem.tif")
e = extent(460000,480000,4555000,4567500)
rr1 = crop(r1,e)
p = levelplot(rr1, scales=list(x=list(at=seq(450000,480000,4000))),
              margin=F, cuts=200,
              col.regions = terrain.colors(350,alpha=1), 
              colorkey=list(space="bottom"),
              xlab="Easting(m)", ylab="Northing(m)")
plot(p)

The plot ends up looking like this:

levelplot

What I cannot figure out is how to increase the space between the colorkey and the x-axis such that the colorkey does not cover the x-axis label.


Solution

  • Add the following:

    par.settings = list(layout.heights=list(xlab.key.padding=1))
    

    Test example:

    x <- seq(pi/4, 5*pi, length.out=100)
    y <- seq(pi/4, 5*pi, length.out=100)
    r <- as.vector(sqrt(outer(x^2, y^2, "+")))
    grid <- expand.grid(x=x, y=y)
    grid$z <- cos(r^2) * exp(-r/(pi^3))
    p <- levelplot(z~x+y, data=grid,
                   margin=F, cuts=200,
                   par.settings=list(layout.heights=list(xlab.key.padding=1)),
                   col.regions=terrain.colors(350, alpha=1), 
                   colorkey=list(space="bottom"),
                   xlab="Easting(m)", ylab="Northing(m)")
    print(p)
    

    levelplot