[GIS] Adding legend title in plot function of R

legendplotr

I use plot function to plot a raster in R. The plot function adds a legend as default. However, the default legend added does not have a title. For instance I need to add the unit such as meters.

I cannot add a title to legend. How can I do it in plot function?

plot(raster, main = "text", sub = "This is elevation", lwd =5, col =my.palette, legend = T )

Best Answer

You can add a title to the legend using legend.args in the call to plot the raster. E.g:

library(raster)    
plot(elevRaster, legend.args = list(text = 'Elevation (m)', side = 4, 
         font = 2, line = 2.5, cex = 0.8))

The above snippet is taken from this question where you can also find a few more expansive examples of how the legend can be formatted (for the raster package): https://stackoverflow.com/questions/9436947/legend-properties-when-legend-only-t-raster-package