Search code examples
rplotrasterr-rasterrastervis

Adding scale labels to levelplot's margins


I'd like to add label showing the values of latitudinal zonal averages to levelplot's gray margin. In the following example, the min and max values for latitudinal means are 286 and 751 respectively. Any suggestion on adding an axis with this information to the margin? enter image description here

library(raster)
library(rasterVis)
r <- raster(system.file("external/test.grd", package="raster"))
levelplot(r, at=seq(100, 1850, by = 250))
# calculating the latitudinal means
rows <- init(r, v='y')
yAve <- zonal(r, rows, fun='mean',na.rm=TRUE)  
summary(yAve)   

Solution

  • Use margin=list(axis=TRUE) to add the min and max values to the margin.

    library(raster)
    library(rasterVis)
    r <- raster(system.file("external/test.grd", package="raster"))
    levelplot(r, at=seq(100, 1850, by = 250), margin=list(axis=TRUE))
    

    You can change the color and fontsize of the margin labels using gpar.

    library(grid)
    levelplot(r, at=seq(100, 1850, by = 250), margin=list(axis=gpar(col = 'black', fontsize = 9)))
    

    Margin labels