Skip to content

raster::plot() common color scale?

10 messages · Paul Hiemstra, Adam Sparks, Andy Bunn +2 more

#
Hi!
I'm doing

par(mfrow=c(2,2))
plot(simr)
plot(simrlisa)
a2 = simr>l$lisamax
plot(l$lisamax)
plot(simr*a2)

where simr, l%lisamax and a2 are raster objects. Is there any way to
get a common color scheme
(i.e., setting common min and max values)?
The color table is the same, but the scaling is independent for each
raster object and cannot be compared.

Thanks

Agus
#
Hi,

I would recommend using one of the more advanced plotting facilities in
R, ggplot. An example:

library(ggplot2)
library(raster)
theme_set(theme_bw())

r = raster(system.file("external/test.grd", package="raster"))

# Convert to data.frame
r_df = as.data.frame(as(r, 'SpatialPixelsDataFrame'))
# 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$valuesx10 = r_df$values*2

# 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()

This is a basic ggplot example of visualizing rasters. I realize that it
is a short example with a lot of things specific to ggplot, but I hope
you can figure out why I use the code that I use. In my view, the
investment in learning to use ggplot is worth it. A good place to start
is the ggplot website [1].

cheers.
Paul

[1] http://had.co.nz/ggplot2/
On 06/09/2011 01:59 PM, Agustin Lobo wrote:

  
    
#
Last time I had to do this I did something like: 

foo <- raster(nrows=20, ncols=20)
values(foo) <- runif(400)
bar <- raster(nrows=20, ncols=20)
values(bar) <- runif(400)
brks <- seq(0,1,by=0.1)
nbrks <- length(brks)-1
plot(foo,breaks=brks,col=rev(terrain.colors(nbrks)),lab.breaks=brks,zlim=c(0,1))
plot(bar,breaks=brks,col=rev(terrain.colors(nbrks)),lab.breaks=brks,zlim=c(0,1))
#
Here is yet another way:

library(raster)
foo <- raster(nrows=20, ncols=20) 
values(foo) <- runif(400) 
bar <- raster(nrows=20, ncols=20) 
values(bar) <- runif(400) 
s <- stack(foo, bar, foo*2)

# first sample large rasters:
x <- sampleRegular(s, 25000, asRaster=T)

# then use spplot
spplot(as(x, 'SpatialGridDataFrame'))


# I have added two functions to raster version 1.8-33 
# a generic function for spplot and Raster objects
# that does the same as the example above in one short line:

spplot(s)

# and a wrapper around ggplot (called gplot),
# based on Paul's example, that allows you to do things like:

theme_set(theme_bw())

gplot(s) + geom_tile(aes(fill = value)) + facet_wrap(~ variable) +
            scale_fill_gradient(low = 'white', high = 'blue') +
coord_equal()


Robert


--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/raster-plot-common-color-scale-tp6457926p6464547.html
Sent from the R-sig-geo mailing list archive at Nabble.com.
3 days later
#
On 06/11/2011 04:38 AM, Robert Hijmans wrote:
Hi Robert,

So gplot performs the data transformation from RasterLayer to
data.frame? For plotting polygons look at ?fortify.SpatialPolygons. This
function translates SpatialPolygons (and SpatialLines) to a data.frame
which can be used by geom_polygon or geom_path. In pseudo-code:

poly_df = fortify(poly_spatial, region = 'some_id')
ggplot(aes(x = x, y = y, fill = z), data = somedata) + geom_tile() +
geom_path(aes(group = group), data = poly_df)

cheers,
Paul

  
    
1 day later
#
Paul,

While this is certainly the most elegant solution, would it work with
really large raster objects? Note that you
make a data.frame, and the most important characteristic of package
raster is its ability to do not incorporate
the actual raster values to the raster objects to save memory.

Would this problem be avoided by the new wrapper provided by Robert?

Thanks

Agus

2011/6/10 Paul Hiemstra <paul.hiemstra at knmi.nl>:
#
Robert,

I've tried with raster_1.8-35
but:

other attached packages:
[1] raster_1.8-35 ggplot2_0.8.9 proto_0.3-8   reshape_0.8.4 plyr_1.4
[6] plotrix_3.2-2 sp_0.9-76     rkward_0.5.6

loaded via a namespace (and not attached):
[1] digest_0.4.2    lattice_0.19-26 tools_2.13.0
Error in rk.record.plot$.save.tlo.in.hP() :
  could not find function "trellis.last.object"
Calls: print ... print -> print.trellis -> printFunction -> <Anonymous>
Error in function (classes, fdef, mtable)  :
  unable to find an inherited method for function "spplot", for
signature "RasterStack"
Calls: spplot -> <Anonymous>
+ gplot(s) + geom_tile(aes(fill = value)) + facet_wrap(~ variable) +
+            scale_fill_gradient(low = 'white', high = 'blue') +
+ coord_equal()
Error: could not find function "gplot"
+ ggplot(s) + geom_tile(aes(fill = value)) + facet_wrap(~ variable) +
+            scale_fill_gradient(low = 'white', high = 'blue') +
+ coord_equal()
Error: ggplot2 doesn't know how to deal with data of class RasterStack
Agus

2011/6/11 Robert Hijmans <r.hijmans at gmail.com>:
#
This works for me. It seems that the new version or raster did not get
loaded properly. Can you close & open R and try again? Perhaps without
loading the previous session. Robert
On Wed, Jun 15, 2011 at 8:12 AM, Agustin Lobo <alobolistas at gmail.com> wrote:
#
Yes, through its "maxpixels" argument (which has a default value).
On Wed, Jun 15, 2011 at 7:59 AM, Agustin Lobo <alobolistas at gmail.com> wrote: