[R-sig-Geo] Adding labels to map with either spplot or maptools (or anything else suggested)
Edzer Pebesma
edzer.pebesma at uni-muenster.de
Wed Apr 7 22:05:06 CEST 2010
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:
> 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
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
--
Edzer Pebesma
Institute for Geoinformatics (ifgi), University of Münster
Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251
8333081, Fax: +49 251 8339763 http://ifgi.uni-muenster.de
http://www.52north.org/geostatistics e.pebesma at wwu.de
More information about the R-sig-Geo
mailing list