[R] fill map with gradient: package?

Paul Murrell p.murrell at auckland.ac.nz
Wed Nov 4 22:56:49 CET 2009


Hi


Thomas Steiner wrote:
> Hi,
> I'd like to fill an existing svg (or png) map with gradient colors.
> In detail: The file
> http://commons.wikimedia.org/wiki/File:Karte_%C3%96sterreich_Bundesl%C3%A4nder.svg
> should be filled with the population density data from this table:
> http://de.wikipedia.org/wiki/%C3%96sterreich#Verwaltungsgliederung
> choosing the right color saturation (or whatever). The final result
> should be something like this map:
> http://commons.wikimedia.org/wiki/Image:Bevoelkerungsdichte_-_Oesterreich.svg
> Is there a package or so for these two tasks (filling and color
> density ploting)?


The 'grImport' package can help with getting the SVG into R (see 
http://www.jstatsoft.org/v30/i04).

First step is to convert the SVG to PostScript (I used InkScape - you 
can play around with how the text comes across, but I'm going to ignore 
that and concentrate on the map regions).

Having done that, the following code loads the map into R and draws it ...

library(grImport)
PostScriptTrace("Austria-Map-withFonts.ps", charpath=FALSE)
map <- readPicture("Austria-Map-withFonts.ps.xml")
grid.picture(map)

... (the orientation may be 90 degrees out and you may get some warnings 
about character encodings;  the former is easy to fix [see below] and 
the latter can just be ignored for now because we are ignoring the 
text).  The next code shows the breakdown of the map into separate 
"paths" ...

grid.newpage()
picturePaths(map)

... from which we can see that the regions are the first 10 paths ...

grid.newpage()
grid.picture(map[1:10], use.gc=FALSE)

At this point, you can use grImport to draw the regions with different 
fill colours, or you can just extract the x,y coordinates of the regions 
and go-it-alone.  The following code takes the latter path, setting up 
10 different colours, and drawing each region using grid.polygon().  The 
orientation is fixed by pushing a rotated viewport first ...


colours <- hcl(240, 60, seq(30, 80, length=10))
grid.newpage()
pushViewport(viewport(angle=-90),
              grImport:::pictureVP(map[1:10]))
mapply(function(p, col) {
            grid.polygon(p$x, p$y, default.units="native",
                         gp=gpar(fill=col))
        },
        regions, colours)


Hope that helps.

Paul


> Thanks for your help,
> Thomas
> 
> ______________________________________________
> 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