[R-sig-Geo] raster::plot() common color scale?

Paul Hiemstra paul.hiemstra at knmi.nl
Fri Jun 10 13:20:39 CEST 2011


 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:
> 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
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo


-- 
Paul Hiemstra, Ph.D.
Global Climate Division
Royal Netherlands Meteorological Institute (KNMI)
Wilhelminalaan 10 | 3732 GK | De Bilt | Kamer B 3.39
P.O. Box 201 | 3730 AE | De Bilt
tel: +31 30 2206 494

http://intamap.geo.uu.nl/~paul
http://nl.linkedin.com/pub/paul-hiemstra/20/30b/770



More information about the R-sig-Geo mailing list