[GIS] How to plot a common colorbar for mutiple raster plots in a single image in R

rraster

I am plotting a spatial map of monthly temperature. I need to keep a common scale for all the months. How to do this in R?

I have included my code below with the generated map. How can I reduce the additional space shown by blue arrows?

library(raster)

png(
  "Image.png",
  width     = 12,
  height    = 10,
  units     = "in",
  res       = 1200,
  point     = 10
)
 par(mar=c(1.5, 1.5, 1.5, 1.5), mfrow=c(3,4), oma = c(4, 4, 2, 2))

for (i in 1:12){

  month_col = i+2 #Since months start from 3 column  
  r = rasterFromXYZ(testing[,c(2,1,month_col)])
  plot(r)

}

dev.off()

enter image description here

Best Answer

I see you make a for loop to print each time, if you do this, each time it will print a scale; you may set it to false and at the end place it;

For a job like the one you want to do i used

library(gplot)

which, like the "raster" package is also created by R: Hijmans it facets like ggplot2, prints a single scalebar and you may also give spatialpolygonsdataframes to it.

For reducing space, pass a bbox() or extent() argument to your plot function, this fits the plotting region to your spatial data