[R-sig-Geo] plot a variable in a SpatialPolygons object
Roger Bivand
Roger.Bivand at nhh.no
Thu Aug 21 08:41:00 CEST 2008
On Thu, 21 Aug 2008, Massimo Ventrucci wrote:
> Roger,
> sorry if I reply so late to your, instead, really quick suggestion at my
> problem, actually I revised the question and I am not yet able to do
> that plot. I try to explain clearly my problem.
>
> I need to plot a map, for instance, in the same way I can plot it with
> winbugs from the mapping tool menu. So I need to plot a value for each
> region of my map and to colour regions characterized by higher values. I
> have the map in a Spatial Polygons object, I do not have it in shape
> file unfortunately. So I am working with package "sp". Moreover I can
> not use the spplot because I do not have a SpatialPolygonsDataFrame
> object, just a SpatialPolygons. In other words I do not have a value for
> each x-y coords, but I would need a value for each region, hence for
> each polygon belonging to the list of polygons.
Say SP is your SpatialPolygons object, and var is a numeric vector. Is
length(var) == length(slot(SP, "polygons"))
Are the values in var ordered in the same order as the polygons in SP? If
so, you may define the class interval break points and a colour palette as
say:
brks <- quantiles(var, seq(0, 1, 1/7))
cols <- grey.colors(length(brks)-1, 0.4, 0.9)
plot(SP, col=cols[findInterval(var, brks, all.inside=TRUE))
If the lengths agree, but the order doesn't, look at and identify the ID
values in the SpatialPolygons object:
IDs <- sapply(slot(SP, "polygons"), function(x) slot(x, "ID"))
plot(SP)
text(coordinates(SP), labels=IDs, cex=0.7)
Next add the matching IDs values to the values in var as a vector of
names, and:
names(var) <- IDs_in_correct_order
SPDF <- SpataialPolygonsDataFrame(SP, as.data.frame(var)))
to make a SpatialPolygonsDataFrame. Then you can either:
spplot(SPDF, "var")
or
plot(SPDF, col=cols[findInterval(SPDF$var, brks, all.inside=TRUE))
(untried but should work).
Hope this helps,
Roger
>
>
> Please, any suggestion about which is the easiest way to do it, if any?
> Thank you very much in advance
> - massimo
>
> Massimo Ventrucci
> Ph.D Student in Statistics
> Dipartimento di Scienze Statistiche
> Università degli studi di Bologna
> Via Belle Arti 41
> 40126 Bologna-Italy
> _____________________________________
>
> Phone +39(051)264182 (office)
> Fax +39(051)232153
> E-mail massimo.ventrucci at unibo.it
>
> On Mon, 28 Jul 2008, Massimo Ventrucci wrote:
>
>> Dear all,
>
>> sorry my question seems to me quite basic and perhaps is already posted
>> but at a first sight I did not find it. Basically I have a map of
>> contiguous areas in the form of a SpatialPolygons object. In all areas I
>> need to display the value of a binary value (e.g. to color the areas
>> corresponding to value 1). I have to say in a guide to the package
>> "spdep" I found the code to do a map (it was a map of probabilities
>> buillt by using plot coomand after having used probmap command), but in
>> that case the plot is done starting from an object of class polylist if
>> I say well. Instead I need to do a plot starting from a SpatialPolygons
>> object because I do not have the map in other formats.
>
> I'm planning to update the now very obsolete vignette in spdep soon. The
> easiest way to plot a variable from a SpatialPolygonsDataFrame object is
> the spplot method in the sp package (which spdep loads). Just try:
>
> spplot(my_SpolDF, "myvar")
>
> for a first cut.
>
> Base graphics methods, such as plot(), are also available. The best online
> guide may be one of the units at:
>
> http://www.bias-project.org.uk/ASDARcourse/
>
> either "Vizualising Spatial Data" for the description, or your choice of
> the worked examples. There are also code examples and figures on the ASDAR
> book website: www.asdar-book.org; though this is still under construction,
> most things are there.
>
> Roger
>
>>
>> Moreover, in future I will also need to plot values graduated by colors.
>>
>> Please, can you help me?
>> thanks in advance
>> massimo
>>
>> Massimo Ventrucci
>> Ph.D Student in Statistics
>> Dipartimento di Scienze Statistiche
>> Università degli studi di Bologna
>> Via Belle Arti 41
>> 40126 Bologna-Italy
>> _____________________________________
>>
>> Phone +39(051)264182 (office)
>> Fax +39(051)232153
>> E-mail massimo.ventrucci at unibo.it <https://stat.ethz.ch/mailman/listinfo/r-sig-geo>
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at stat.math.ethz.ch <https://stat.ethz.ch/mailman/listinfo/r-sig-geo>
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>
>
--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no
More information about the R-sig-Geo
mailing list