Skip to content

Adding labels to map with either spplot or maptools (or anything else suggested)

3 messages · Misha Spisok, Edzer Pebesma

#
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
#
Misha, you were close. I managed to get somewhat closer with:

plot(gadm)
text(getSpPPolygonsLabptSlots(gadm), labels=regionnames, cex=0.4)

still, you need to provide regionnames in the order of the polygons in
gadm, whether you use plot or spplot, in order to have the right labels.
The command factor orders them alphabetically, unless told otherwise.
The as.character was not needed, as regionnames already was character.

spplot is not an easy path for the plot you want, as it tries to give
colors to polygons in order to communicate something. Of course, you
could make all colors white but then there's still the legend to get rid
of. Plotting text on particular locations can be done by passing an
sp.layout argument - see ?spplot.

Hope this helps,
Misha Spisok wrote:

  
    
#
Thank you for the replies, Edzer and Weidong.

I did the following.  I used 'names(nc2)' (with respect to the
original code in Example 2), then, guessing and confirming that
'NAME_1' contained the region names, I changed the line 'text(...)' to
contain 'NAME_1' instead of 'NAME,' (as given below the output from
'names(nc2)').
[1] "ID_0"       "ISO"        "NAME_0"     "ID_1"       "NAME_1"
 [6] "VARNAME_1"  "NL_NAME_1"  "HASC_1"     "CC_1"       "TYPE_1"
[11] "ENGTYPE_1"  "VALIDFR_1"  "VALIDTO_1"  "REMARKS_1"  "Shape_Leng"
[16] "Shape_Area"

text(centroids, label=nc2$NAME_1)

As a follow up...

Is there a way to enlarge the map, rather than adjusting the font size
(via 'cex')?
Is there a way to rotate the map?

Thanks again,

Misha
On Wed, Apr 7, 2010 at 12:19 PM, Misha Spisok <misha.spisok at gmail.com> wrote: