Misha Spisok
2010-Apr-06 18:54 UTC
[R] Adding labels on maps (using sp, maptools, or something else suggested)
Hello, I would like to place region names (or abbreviations if space is too limited) on a map. I have tried the following, copying and adapting various examples I have seen but without success. The first example is self-contained, but the second one requires the .shp file, which can retrieved at http://www.gadm.org/country. ----- 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 want uncolored 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)) 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 in either the .shp or .dbf file. Best, misha