[R] Fine Tuning Country Map

Marcio Pupin Mello mello at ieee.org
Mon Nov 26 18:11:38 CET 2012


Lorenzo,
	I'd suggest posting such questions to the R-sig-geo list, which seems 
more suitable.
	The book "Applied Spatial Data Analysis with R" written by Roger 
Bivand, Edzer Pebesma and Virgilio Gómez-Rubio has exactly what you want.
	http://www.amazon.com/Applied-Spatial-Data-Analysis-Use/dp/0387781706
	Package classInt can help with colouring;
	and about the legend: "The location may also be specified by setting x 
to a single keyword from the list "bottomright", "bottom", "bottomleft", 
"left", "topleft", "top", "topright", "right" and "center". This places 
the legend on the inside of the plot frame at the given location. 
Partial argument matching is used. The optional inset argument specifies 
how far the legend is inset from the plot margins. If a single value is 
given, it is used for both margins; if two values are given, the first 
is used for x- distance, the second for y-distance."
	Good luck!

Marcio
www.dsr.inpe.br/~mello
	

On 8/13/12 11:33 PM, Lorenzo Isella wrote:
> Dear All,
> Please see the short script at the end of the email, which I assembled
> looking for bits and pieces on the web.
> It essentially does what I need: it plots several countries as a
> color-coded map.
> I just would like to fine-tune a bit the final image, in particular
>
> (1) Select my own colors for "painting" the countries (i.e. associate
> manually a color to every level)
> (2) Be able to control the position of the legend and the size of the
> character used in the legend itself.
>
> Any suggestion is welcome.
> Cheers
>
> Lorenzo
>
> ################################################à
>
> ## you will need the sp-package
> library('sp')
>
> ## load a file from GADM (you just have to specify the countries
> "special part" of the file name, like "ARG" for Argentina. Optionally
> you can specify which level you want to have
> loadGADM <- function (fileName, level = 0, ...) {
>      load(url(paste("http://gadm.org/data/rda/", fileName, "_adm",
> level, ".RData", sep     = "")))
>      gadm
> }
>
> ## the maps objects get a prefix (like "ARG_" for Argentina)
> changeGADMPrefix <- function (GADM, prefix) {
>      GADM <- spChFIDs(GADM, paste(prefix, row.names(GADM), sep = "_"))
>      GADM
> }
>
> ## load file and change prefix
> loadChangePrefix <- function (fileName, level = 0, ...) {
>      theFile <- loadGADM(fileName, level)
>      theFile <- changeGADMPrefix(theFile, fileName)
>      theFile
> }
>
> ## this function creates a SpatialPolygonsDataFrame that contains all
> maps you specify in "fileNames".
> ## E.g.:
> ## spdf <- getCountries(c("ARG","BOL","CHL"))
> ## plot(spdf) # should draw a map with Brasil, Argentina and Chile on it.
> getCountries <- function (fileNames, level = 0, ...) {
>      polygon <- sapply(fileNames, loadChangePrefix, level)
>      polyMap <- do.call("rbind", polygon)
>      polyMap
> }
>
> spdf <- getCountries(c("ITA","CHE","FRA", "DEU","BEL", "LUX"))
>
> AP <- c("SLS","SLS", "NWS", "NSLS", "NSLS", "NWS")
>
> spdf$AP <- as.factor(AP)
>
> png("many-countries.png")
> ## print(spplot(spdf, "NAME_ENGLI"))
> print(spplot(spdf, "AP"))
> ## plot(spdf)
> dev.off()
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list