Search code examples
rrasterterra

change the size and orientation of legend title while plotting raster


library(terra)
library(RColorBrewer)

# sample polygon
p <- system.file("ex/lux.shp", package="terra")
p <- terra::vect(p)

# sample raster
r <- system.file("ex/elev.tif", package="terra")
r <- terra::rast(r)
r <- terra::crop(r, p , snap = "out", mask = T)

terra::plot(r,
           col = brewer.pal(9, "pastel1"),
           cex.main = 2,
           smooth = T,
           legend = T,
           plg = list(title = "Score"),
           axes = TRUE,
           mar=c(3,3,3,6))
plot(p, add = T)

How do I change the size and orientation of the legend title 'Score'. I want to orient the title so that it is vertical and follows along the legend and also change the size of the legend title?


Solution

  • You can add text whereever you want. For example

    library(terra)
    p <- terra::vect(system.file("ex/lux.shp", package="terra"))
    r <- terra::rast(system.file("ex/elev.tif", package="terra"))
    
    plot(r, mar=c(3,3,3,6))
    text(x=6.77, y=49.95, "Score", srt=-90, cex=2, xpd=NA, pos=4)
    lines(p)
    

    enter image description here