[R-sig-Geo] Overlaying rasters in spplot
Nagle, Nicholas
nnagle at utk.edu
Tue Nov 29 13:52:29 CET 2011
Hi Chris -
I don't know how to do what you want with spplot, but I've done it with ggplot. Overlays and colors are more intuitive to me in ggplot. Maybe this will help?
I recently had to plot data from the NLCD. It has a colortable in the metadata, and people are used to seeing the data with exactly those colors. Here's how I did it in ggplot.
# This was a copy and past of the color table from the metadata. I wrapped it in cat and saved it to a file.
cat("
0 0.00000000000 0.00000000000 0.00000000000
11 0.27843137255 0.41960784314 0.62745098039
12 0.81960784314 0.86666666667 0.97647058824
21 0.86666666667 0.78823529412 0.78823529412
22 0.84705882353 0.57647058824 0.50980392157
23 0.92941176471 0.00000000000 0.00000000000
24 0.66666666667 0.00000000000 0.00000000000
31 0.69803921569 0.67843137255 0.63921568628
41 0.40784313726 0.66666666667 0.38823529412
42 0.10980392157 0.38823529412 0.18823529412
43 0.70980392157 0.78823529412 0.55686274510
51 0.64705882353 0.54901960784 0.18823529412
52 0.80000000000 0.72941176471 0.48627450980
71 0.88627450980 0.88627450980 0.75686274510
72 0.78823529412 0.78823529412 0.46666666667
73 0.60000000000 0.75686274510 0.27843137255
74 0.46666666667 0.67843137255 0.57647058824
81 0.85882352941 0.84705882353 0.23921568628
82 0.66666666667 0.43921568628 0.15686274510
90 0.72941176471 0.84705882353 0.91764705882
95 0.43921568628 0.63921568628 0.72941176471", file='Anderson.colors.tab')
color.table <- read.table('Anderson.colors.tab')
names(color.table) <- c('Value','R','G','B')
row.names(color.table) <- color.table$Value
colors <- hex(RGB(color.table$R,color.table$G,color.table$B))
colors <- data.frame(colors,row.names=row.names(color.table))
# In my case, grid3 was a SpatialPixelsDataFrame object
grid.gdf <- data.frame(cbind(coordinates(grid3),value=(grid3$value)))
grid.gdf$value <- factor(grid.gdf$value,levels=row.names(color.table))
gplt <- ggplot()+geom_tile(aes(x=x,y=y,fill=value),data=grid.gdf)+scale_fill_manual(values=colors$colors)+coord_equal()
It's easy to set the alpha transparency level in ggplot. If you don't have categorical data like this, but continuous data, I usually still use ggplot, but I turn the continuous values into a categorical variable, and then assign colors using the RColorBrewer package.
Good Luck,
Nicholas
Department of Geography
University of Tennessee
Date: Mon, 28 Nov 2011 16:54:55 -0800
From: Chris Fowler <chris at csfowler.com>
To: r-sig-geo at r-project.org
Subject: [R-sig-Geo] Overlaying rasters in spplot
Message-ID:
<CAFK3DV-tBPhzr+049R8w=bzmc5RojSCST4NXqKEXbU8=3gOSAg at mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
I am still having trouble with some raster overlays.
My trouble now is a result of my not really understanding how layers
and sp.layout work in the spplot function
I go through a whole mess of steps to get two rasters of identical
extent and dimension (One is a google map tile, the other my own data)
Plotting them using the default plot() method within raster, however
doesn't allow me to have the color palette and breaks I want, so at
the suggestion of one of the threads on this list I have converted my
raster objects to SpatialGridDataFrame objects.
I can plot these objects and get exactly what I want using the following code:
layer1<-spplot(raster1,"values",at=brks1,col.regions=pal1,colorkey=FALSE)
layer1
layer2<-spplot(raster2, "values", at=brks2,col.regions=pal2)
layer2
from reading the documentation on spplot() from sp package I should be
able to use the sp.layout attribute to map the second of these layers
with a transparency of say .5, but I have not been able to get there.
I am hoping that this is enough of a syntax question that the experts
here can help without a full worked example--my apologies if this is
not the case.
Thanks in advance,
Chris
Christopher S. Fowler PhD.
206.920.1686
http://csfowler.com
On Nov 29, 2011, at 6:00 AM, <r-sig-geo-request at r-project.org>
wrote:
> Date: Mon, 28 Nov 2011 16:54:55 -0800
> From: Chris Fowler <chris at csfowler.com>
> To: r-sig-geo at r-project.org
> Subject: [R-sig-Geo] Overlaying rasters in spplot
> Message-ID:
> <CAFK3DV-tBPhzr+049R8w=bzmc5RojSCST4NXqKEXbU8=3gOSAg at mail.gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1
>
> I am still having trouble with some raster overlays.
> My trouble now is a result of my not really understanding how layers
> and sp.layout work in the spplot function
>
> I go through a whole mess of steps to get two rasters of identical
> extent and dimension (One is a google map tile, the other my own data)
> Plotting them using the default plot() method within raster, however
> doesn't allow me to have the color palette and breaks I want, so at
> the suggestion of one of the threads on this list I have converted my
> raster objects to SpatialGridDataFrame objects.
>
> I can plot these objects and get exactly what I want using the following code:
> layer1<-spplot(raster1,"values",at=brks1,col.regions=pal1,colorkey=FALSE)
> layer1
> layer2<-spplot(raster2, "values", at=brks2,col.regions=pal2)
> layer2
>
> from reading the documentation on spplot() from sp package I should be
> able to use the sp.layout attribute to map the second of these layers
> with a transparency of say .5, but I have not been able to get there.
>
> I am hoping that this is enough of a syntax question that the experts
> here can help without a full worked example--my apologies if this is
> not the case.
>
> Thanks in advance,
>
> Chris
>
> Christopher S. Fowler PhD.
> 206.920.1686
> http://csfowler.com
More information about the R-sig-Geo
mailing list