[R-sig-Geo] Adding labels to map with either spplot or maptools (or anything else suggested)

Misha Spisok misha.spisok at gmail.com
Wed Apr 7 21:19:36 CEST 2010


Hello,

In brief: I want to label regions on a map.

I would like to place region names (or abbreviations, if space is too
limited) on a map.  I have tried the examples pasted below, having
copied and adapted various examples I have found but without success
(not to mention countless failed experiments).  The first example,
using 'sp,' is self-contained, but the second one, using 'maptools,'
requires the .shp file, which can retrieved at
http://www.gadm.org/country.  I didn't comment the code because I
don't understand anything beyond trivial 'R' stuff.

-----     Example 1     -----
library(sp)
con <- url("http://gadm.org/data/rda/ITA_adm1.RData")
print(load(con))
close(con)

regionnames <- c("Piemonte", "Valle d'Aosta", "Liguria", "Lombardia",
       "Trentino-Alto Adige/Südtirol", "Veneto", "Friuli-Venezia Giulia",
       "Emilia-Romagna", "Toscana", "Umbria", "Marche", "Lazio",
"Abruzzo", "Molise",
       "Campania", "Puglia", "Basilicata", "Calabria", "Sicilia", "Sardegna")

gadm$regionnames <- as.factor(regionnames)
col = rainbow(length(levels(gadm$regionnames)))
spplot(gadm, "regionnames", col.regions=col, main="Italian Regions")

This gets me a map with regions colored, but I would like uncolored
(i.e., plain white) regions with the labels in 'regionnames' placed in
the appropriate region.  Furthermore, the colors do not correctly
match the names.  I suppose I could manually re-order my regionnames
vector, but that seems avoidable in a way that I cannot see.  Finally,
I do not want the legend, as it would be meaningless for my use.

-----     Example 2     -----
library(maptools)
nc2 <- readShapePoly("ITA_adm1.shp")
plot(nc2)
invisible(text(getSpPPolygonsLabptSlots(nc2),
labels=as.character(regionnames), cex=0.4))

I also tried the following assignments for 'nc2':
nc2 <- readShapePoly("ITA_adm1.shp"[1], proj4string=CRS("ITA_adm1.dbf"))
nc2 <- readShapePoly("ITA_adm1.shp"[1], proj4string=CRS("+proj=longlat
+datum=NAD27"))

...but these were no more helpful.

This gets me a map with regions outlined (as desired), but the names
don't land where I want them to.  A relatively "klunky" solution is to
just re-order them manually when I declare

regionnames <- c( etc ),

but I'd like to learn how to use what seems to be already available
functionality in either the .shp or .dbf file.

Finally, I also tried to append this (copied then altered) code after
'plot(nc2)':
centroids <- coordinates(nc2)
text(centroids, label=nc2$NAME)

Thank you for taking the time to read this and for any help you may offer.

Best,

misha



More information about the R-sig-Geo mailing list