[R-sig-Geo] raster::plot() common color scale?
Paul Hiemstra
paul.hiemstra at knmi.nl
Tue Jun 14 09:52:45 CEST 2011
On 06/11/2011 04:38 AM, Robert Hijmans wrote:
>> Is there any way to
>> get a common color scheme
>> (i.e., setting common min and max values)?
> 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())
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
> 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.
>
> _______________________________________________
> 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