0
votes

I have a rasterstack, which I am trying to plot in R by spplot:

rasterstack
class      : RasterStack 
dimensions : 2803, 5303, 14864309, 3  (nrow, ncol, ncell, nlayers)
resolution : 0.008333333, 0.008333333  (x, y)
extent     : 60.85, 105.0417, 15.95833, 39.31667  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
names      :   layer.1,   layer.2,   layer.3 
min values : -1.819715, -1.800774, -1.835778 
max values :    6.5533,    6.5533,    6.5533 

I want a colorscale from red to blue, with white in the centre. I achieved the color scale through this code:

devtools::source_gist('306e4b7e69c87b1826db')
plot2<-diverge0(rasterstack, ramp='RdBu')

However when plotted the spatial map shows very light colors, which cannot be visually easy to see. I would like to make a colorscale that is much darker and visually visible.

colorscales

Reproducible raster:

library(raster)
r1 <- r2 <- r3 <- raster(nrow=10, ncol=10)
values(r1) <- runif(ncell(r1))
values(r2) <- runif(ncell(r2))
values(r3) <- runif(ncell(r3))
s <- stack(r1, r2, r3)

Note that the reproducible rasters do not have the same raster stack minimum and maximum values, I would appreciate it if someone could guide by my actual rasterstack min and max values. (Secondly I would like the colorscale to go from red white to blue)

2

2 Answers

1
votes

Your color scale (-1 to 7) is actually correct, as e.g. the darkness of -1 corresponds to the one of +1. That's an important aspect of a diverging scale. If you want to keep zero in the center, you have to expand it to a common absolute extreme value (-7 to 7 in your case). After some digging I found this more elegant solution. Zero is now in the center of the scale.

library(raster)
#> Lade nötiges Paket: sp
r1 <- r2 <- r3 <- raster(nrow=10, ncol=10)
values(r1) <- runif(ncell(r1),-5,5)        # I adjusted your stack min max to [-1;7]
values(r2) <- runif(ncell(r2),-1,4)
values(r3) <- runif(ncell(r3),0,7)
s <- stack(r1, r2, r3)
print(cellStats(s, range))
#>        layer.1    layer.2   layer.3
#> [1,] -4.999763 -0.9014062 0.2718563
#> [2,]  4.891400  3.9860373 6.9711417

cols <- c('#ff0000', '#ffffff', '#002bff')   # red, white, blue
c.pal <- colorRampPalette(cols)              # interpolate between the 3 colors

s.max <- max(abs(cellStats(s,stat = unique)))  # get absolute max of stack

# make 100 even breaks with 0 in center
library(lattice)
breaks <- do.breaks(c(-s.max,s.max), 100)

spplot(s, col.regions = c.pal, at = breaks,
       colorkey = list(col = c.pal, at = breaks))

Created on 2020-03-28 by the reprex package (v0.3.0)

Hope this could clarify things!

1
votes

Here is a solution that uses rasterVis and ggplot2 to get your desired color scale. scale_fill_gradientn() and facet_wrap() take care that your scale fits all rasters in the stack.

library(raster)
#> Lade nötiges Paket: sp
r1 <- r2 <- r3 <- raster(nrow=10, ncol=10)
values(r1) <- runif(ncell(r1),-.2,.2)        # I adjusted your stack min max
values(r2) <- runif(ncell(r2),-.3,.3)
values(r3) <- runif(ncell(r3),-.5,.5)
s <- stack(r1, r2, r3)

library(ggplot2)
library(rasterVis)
#> Lade nötiges Paket: lattice
#> Lade nötiges Paket: latticeExtra
#> Lade nötiges Paket: RColorBrewer
#> 
#> Attache Paket: 'latticeExtra'
#> The following object is masked from 'package:ggplot2':
#> 
#>     layer
cols <- c('#ff0000', '#ffffff', '#002bff')   # red, white, blue
gplot(s) + 
  geom_tile(aes(fill=value)) +
  scale_fill_gradientn(colors = cols, aesthetics = "fill") +
  coord_equal() +
  facet_wrap(~variable)

plot image is here: https://i.imgur.com/u50wPkK.png

Created on 2020-03-28 by the reprex package (v0.3.0)

Hope that helps!