[R-sig-Geo] Choropleth map
Oscar Perpiñan
oscar.perpinan at upm.es
Thu Jul 3 23:10:22 CEST 2014
Hi,
I propose the code below to solve your problem. By the way, I have
found your shapefiles here:
https://github.com/briatte/kmaps/tree/master/maps/regions.
Best,
Oscar.
library(sp)
library(maptools)
nuts <- readShapePoly(fn="NUTS_RG_60M_2006")
## no data provided, so I have to produce a toy example
someNuts <- sample(nuts$NUTS_ID, 300)
Alldata <- data.frame(nuts3 = someNuts, MEt = rnorm(6000))
Value <- aggregate(MEt ~ nuts3, data = Alldata, FUN = mean)
names(Value) <- c("NUTS_ID","ValueMEt")
## Match polygons with data
idx <- match(nuts$NUTS_ID, Value$NUTS_ID)
## Places without information
idxNA <- which(is.na(idx))
##Information to be added to the SpatialPolygons object
dat2add <- Value[idx, ]
## spCbind uses row names to match polygons with data
## First, extract polygon IDs
IDs <- sapply(nuts at polygons, function(x)x at ID)
## and join with the SpatialPolygons
row.names(dat2add) <- IDs
datPols <- spCbind(nuts, dat2add)
## Drop those places without information
datPols <- datPols[-idxNA, ]
## Display variable using 'spplot'
spplot(datPols["ValueMEt"])
-----------------------------------------------------------------
Oscar Perpiñán Lamigueiro
Dpto. Ingeniería Eléctrica (ETSIDI-UPM)
Grupo de Sistemas Fotovoltaicos (IES-UPM)
URL: http://oscarperpinan.github.io
Twitter: @oscarperpinan
More information about the R-sig-Geo
mailing list