[R-sig-Geo] Plotting polygons on top of a levelplot

Edzer Pebesma edzer.pebesma at uni-muenster.de
Mon Aug 11 23:21:57 CEST 2008


No, it's me, I only read half of your question. Try something like

spplot(spdf["dist"], sp.layout=list("sp.polygons", river))

or the incremental build, in lattice style, of

spplot(spdf["dist"])
library(lattice)
spplot(spdf["dist"])
trellis.focus("panel", 1, 1)
sp.polygons(river)
trellis.unfocus()

which will put the river on the foreground. This can be obtained in a 
single pass with

spplot(spdf["dist"], sp.layout=list("sp.polygons", river, first=FALSE))

I slowly get the feeling that spplot needs a bit more documentation, or 
examples.
--
Edzer

Ryan Anderson wrote:
> That likely gives me the control over the color ramp that I am looking
> for, but I still have trouble plotting the vector layer over the top of
> the resulting image plot.
> If I do:
>  spplot(spdf["dist"],col.regions=heat.colors(400), at = 0:10/10)
>  plot(river, add = TRUE)
> I still get the plot.new has not been called yet error after I attempt to
> add the river data.  What am I doing wrong?
>
> Thanks,
> Ryan
>
>
>   
>> Ryan, you're probably looking for something like
>>
>> spplot(spdf["dist"],col.regions=heat.colors(400), at = 0:10/10)
>>
>> spplot is a wrapper around lattice functions for spatial data, and tries
>> to pass on all the lattice controls.
>> --
>> Edzer
>>
>> Ryan Anderson wrote:
>>     
>>> I have some gridded spatial data that I want to plot with a vector
>>> overlay
>>> on top.  The following reproducible code makes a figure similar to the
>>> one
>>> I am ultimately trying to produce:
>>>
>>> library(sp)
>>> library(fields)
>>> data(meuse.grid)
>>> data(meuse.riv)
>>> coords <- SpatialPixels(SpatialPoints(meuse.grid[, c("x","y")]))
>>> spdf <- SpatialPixelsDataFrame(coords, meuse.grid)
>>> fullgrid(spdf)<-TRUE
>>> river<-SpatialPolygons(list(Polygons(list(Polygon(meuse.riv)),"meuse.riv")))
>>> par(mar = c(5,5,5,8), col.axis = 1)
>>> ramp = heat.colors(400)
>>> image(spdf, "dist", axes = TRUE, col = ramp)
>>> plot(river, add = TRUE)
>>> zvals<-spdf$dist
>>> zvals<-zvals[!is.na(zvals)]
>>> image.plot(zlim = c(min(zvals), max(zvals)), nlevel = 256, legend.only =
>>> TRUE, col = ramp)
>>>
>>> I need more control over the legend and the color ramp than I know how
>>> to
>>> achieve with this code.  I can create the plot I want with levelplot by
>>> setting the 'at' component 'colorkey', but when I try to overlay the
>>> vector file I get an error.  Here is a simplified version of the code I
>>> tried:
>>>
>>> #try a levelplot
>>> library(lattice)
>>> x11()
>>> par(mar = c(5,5,5,8))
>>> x<-spdf$x
>>> y<-spdf$y
>>> z<-spdf$dist
>>> levelplot(z~x+y)
>>> plot(river, add = TRUE)
>>> #Gives Error in polygon(x = x, y = y, border = border, col = col, lty =
>>> #lty,  :
>>> #  plot.new has not been called yet
>>>
>>> I need to either find a way to control the mapping of data values to the
>>> color ramp more precisely in my call to image or image.plot or to find a
>>> way to plot a ploygon over the top of a levelplot.
>>>
>>> My color ramp goals more specifically: my grid contains both positive
>>> and
>>> negative values, and they are not necessarily symmetrically distributed
>>> around zero.  I want to plot them so that zero is white, negative values
>>> are symbolized with a ramp from white to blue (with darker shades for
>>> lower values) and positive values are symbolized with a ramp from white
>>> to
>>> red (with darker shades for higher values).
>>>
>>> Thanks for your help!
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo at stat.math.ethz.ch
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>
>>>       
>> --
>> Edzer Pebesma
>> Institute for Geoinformatics (ifgi), University of Münster,
>> Weseler Straße 253, 48151 Münster, Germany.  Phone: +49 251
>> 8333081, Fax: +49 251 8339763  http://ifgi.uni-muenster.de/
>>
>>
>>     
>
>   

-- 
Edzer Pebesma
Institute for Geoinformatics (ifgi), University of Münster,
Weseler Straße 253, 48151 Münster, Germany.  Phone: +49 251
8333081, Fax: +49 251 8339763  http://ifgi.uni-muenster.de/




More information about the R-sig-Geo mailing list