[R-sig-Geo] points on polygons

Roger Bivand Roger.Bivand at nhh.no
Thu Oct 16 09:29:43 CEST 2008


On Wed, 15 Oct 2008, Thomas Lumley wrote:

> On Wed, 15 Oct 2008, hadley wickham wrote:
>
>>  It's relatively easy to do this with ggplot2 (the tricky part is
>>  converting the polygons to a data frame) - I can provide some code if
>>  you're interested.
>> 
>
> That would be great.
>

A direct spplot draft, avoiding converting the geometries, would be 
something like:

library(sp)
library(rgdal)
# downloaded from the site you specified
MMSA <- readOGR(".", "BRFSS_MMSA_2007")
BRFSS <- readOGR(".", "brfss_state_2007_download")
library(classInt)
# make shared class intervals - "jenks" is similar to GIS "natural breaks"
cI <- classIntervals(BRFSS$X4409_3, n=5, style="jenks")
x <- MMSA$X4409_3
is.na(x) <- x < 1
range(x, na.rm=TRUE)
brks <- cI$brks
# manipulate breaks to span MMSA data
brks[1] <- range(x, na.rm=TRUE)[1]
brks[length(brks)] <- range(x, na.rm=TRUE)[2]
library(RColorBrewer)
pal <- brewer.pal(length(brks)-1, "YlOrBr")
spl0 <- list("sp.points", MMSA[MMSA$X4409_3 < 1,], pch=21, col=1,
   fill="grey")
# break out MMSA NAs
spl1 <- list("sp.points", MMSA[MMSA$X4409_3 > 0,], pch=21, col=1,
   fill=pal[findInterval(MMSA$X4409_3, brks, all.inside=TRUE)])
# colour MMSAs
spplot(BRFSS, "X4409_3", at=brks, col.regions=pal, sp.layout=list(spl0,
   spl1))

These are using the data from the shapefiles, I suspect that the values in 
the CSV files are being used in the online display, and that the two 
differ (not only in giving one digit after the decimal point, but also 
there are 183 in the shapefile and 184 in the CSV file).

I guess it could be automated and made more robust.

Hope this helps,

Roger

> 	-thomas
>
> Thomas Lumley			Assoc. Professor, Biostatistics
> tlumley at u.washington.edu	University of Washington, Seattle
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> 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