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

Paul Hiemstra p.hiemstra at geo.uu.nl
Tue Aug 12 18:12:26 CEST 2008


Edzer Pebesma wrote:
> 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))
Edzer,

The first = argument is not documented when I type ?sp.polygons or 
?panel.gridplot. You can only see it if you look at the source code of 
panel.gridplot. A very flexible way of adding layout objects is to 
define the panel function directly like (as shown on the R Wiki 
(http://wiki.r-project.org/rwiki/doku.php?id=tips:spatial-data:spatial_data_visualization)):

spplot(grid_object, "layer_to_plot",
    panel = function(...) {
        panel.gridplot(...)
        sp.polygons(polygon_object)
        sp.points(point_object)
    }

That was my solution to draw the polygons on top of the grid. I started 
a Spatial Data Visualization topic a the R Wiki (see the link above), 
maybe if people keep expanding that with useful topics and examples that 
would provide a place that makes plotting spatial objects a more 
accessible topic.

cheers,
Paul
>
> 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/
>>>
>>>
>>>     
>>
>>   
>


-- 
Drs. Paul Hiemstra
Department of Physical Geography
Faculty of Geosciences
University of Utrecht
Heidelberglaan 2
P.O. Box 80.115
3508 TC Utrecht
Phone: 	+31302535773
Fax:	+31302531145
http://intamap.geo.uu.nl/~paul




More information about the R-sig-Geo mailing list