[R-sig-Geo] Summary: how to plot several raster layers on the same page and with the same color scale

Oscar Perpiñan Lamigueiro oscar.perpinan at upm.es
Wed Jul 6 14:19:30 CEST 2011

```Hi,

Just to add one more solution. It uses the rasterVis package (at
R-Forge now and should be at CRAN today).

library(raster)
library(rasterVis)

levelplot(s, par.settings=RdBuTheme)

More examples here: http://rastervis.r-forge.r-project.org/

Best,

Oscar.

-------------------
Oscar Perpiñán Lamigueiro
Dpto. Ingeniería Eléctrica
EUITI-UPM

http://procomun.wordpress.com

-----------------------------------------
>I summarize here the different solutions I got to my question regarding
>how to plot several raster layers on the same page and with the same
>color scale.
>Sorry it took a long time, I was interrupted by something else for
>weeks. Hope this is useful.
>Many thanks to all.
>
>
>require(raster)
>require(ggplot2)
>require(plotrix)
>require(rasterVis)
>require(lattice)
>
>foo1 <- foo2 <- foo3<- foo4 <- raster(nrows=20, ncols=20)
>values(foo1) <- runif(400,-100.100)
>values(foo2) <- runif(400,-150,100)
>values(foo3) <- runif(400,0,50)
>values(foo4) <- runif(400,-100,150)
>extent(foo1) = extent(foo2) = extent(foo3) = extent(foo4) =
>extent(c(0,20,0,20))
>
>
>#Based on comment by Jeremy Raw and Andy.Bunn at wwu.edu
>par(mfrow=c(2,2))
>plot(foo1,col=rev(heat.colors(32)),zlim=c(-150,150))
>plot(foo2,col=rev(heat.colors(32)),zlim=c(-150,150))
>plot(foo3,col=rev(heat.colors(32)),zlim=c(-150,150))
>plot(foo4,col=rev(heat.colors(32)),zlim=c(-150,150))
>
>#Note by ALOBO: simplest solution
>
>#Based on comment by Adam Sparks
>require(plotrix)
>minValue = -150
>maxValue = 150
>colstep=10
>brk = c(minValue, maxValue, by = colstep)
>brk = seq(minValue, maxValue, by = colstep)
>par(mfrow=c(2,2))
>plot(foo1, breaks = brk, col = rev(heat.colors(30)), axes = FALSE,
>xlab = '', ylab = '', legend = FALSE)
>color.legend(xl=20.5,yb=1,xr=21.5,yt=18, legend = seq(minValue,
>maxValue, by = 3*colstep),
>col="black")
>plot(foo2, breaks = brk, col = rev(heat.colors(30)),legend = FALSE,
>xlab = '', ylab = '',)
>color.legend(xl=20.5,yb=1,xr=21.5,yt=18, legend = seq(minValue,
>maxValue, by = 3*colstep),
>col="black")
>plot(foo3, breaks = brk, col = rev(heat.colors(30)),legend = FALSE,
>xlab = '', ylab = '',)
>color.legend(xl=20.5,yb=1,xr=21.5,yt=18, legend = seq(minValue,
>maxValue, by = 3*colstep),
>col="black")
>plot(foo3, breaks = brk, col = rev(heat.colors(30)),legend = FALSE,
>xlab = '', ylab = '',)
>color.legend(xl=20.5,yb=1,xr=21.5,yt=18, legend = seq(minValue,
>maxValue, by = 3*colstep),
>col="black")
>
>
>#Note by ALOBO: in practice, you must use hist() to determine
>meaningful min and max values in both cases
>
>
>#Based on comment by  paul.hiemstra at knmi.nl
>library(ggplot2)
>library(raster)
>theme_set(theme_bw())
>
>r = foo1
>
># Convert to data.frame
>r_df = as.data.frame(as(r, 'SpatialPixelsDataFrame'))
>print(str(r_df))
># create new column 10 times larger
># here you could extract data from other grids that
># you want to show at the same time
>r_df\$foo2 = values(foo2)
>r_df\$foo3 = values(foo3)
>r_df\$foo4 = values(foo4)
>
>print(str(r_df))
>
># Reshape the data for ggplot
>plotData = melt(r_df, id.vars = c('x','y'))
>
>ggplot(aes(x = x, y = y), data = plotData) +
>    geom_tile(aes(fill = value)) + facet_wrap(~ variable) +
>    scale_fill_gradient(low = 'white', high = 'blue') +
>    coord_equal()
>
>#Note by ALOBO: while this is the most aesthetic solution, would it
>work for large raster objects?
>
>
>#Based on comment by Robert Hijmans:
>#Requires raster version >= 1.8-33 and rasterVis
>
>s <- stack(foo1,foo2, foo3,foo4)
>spplot(s)
>theme_set(theme_bw())
>gplot(s) + geom_tile(aes(fill = value)) + facet_wrap(~ variable) +
>           scale_fill_gradient(low = 'white', high = 'blue') +
> coord_equal()
>
>#Note by ALOBO: assumed it automatically makes a sampleRegular() for
>large raster objects?
>#In such a case, it would combine aesthetic and performance

```