[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 22:44:30 CEST 2010
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)').
> 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:
> 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