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
raster::plot() common color scale?
10 messages · Paul Hiemstra, Adam Sparks, Andy Bunn +2 more
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
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20110610/4bcf5667/attachment.pl>
-----Original Message----- From: r-sig-geo-bounces at r-project.org [mailto:r-sig-geo-bounces at r- project.org] On Behalf Of Agustin Lobo Sent: Thursday, June 09, 2011 7:00 AM To: r-sig-geo; Robert J. Hijmans Subject: [R-sig-Geo] raster::plot() common color scale? 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.
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))
Thanks Agus
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
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())
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:
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
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>:
?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
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
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)) s <- stack(foo1,foo2,foo3,foo4)
spplot(as(x, 'SpatialGridDataFrame'))
Error in rk.record.plot$.save.tlo.in.hP() : could not find function "trellis.last.object" Calls: print ... print -> print.trellis -> printFunction -> <Anonymous>
spplot(s)
Error in function (classes, fdef, mtable) : unable to find an inherited method for function "spplot", for signature "RasterStack" Calls: spplot -> <Anonymous>
theme_set(theme_bw())
+ 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"
theme_set(theme_bw())
+ 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>:
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()) 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
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:
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
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)) s <- stack(foo1,foo2,foo3,foo4)
spplot(as(x, 'SpatialGridDataFrame'))
Error in rk.record.plot$.save.tlo.in.hP() : ?could not find function "trellis.last.object" Calls: print ... print -> print.trellis -> printFunction -> <Anonymous>
spplot(s)
Error in function (classes, fdef, mtable) ?: ?unable to find an inherited method for function "spplot", for signature "RasterStack" Calls: spplot -> <Anonymous>
theme_set(theme_bw())
+ 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"
theme_set(theme_bw())
+ 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>:
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()) 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
Would this problem be avoided by the new wrapper provided by Robert?
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:
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>:
?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