Thursday, January 5, 2012

Coat of arms of Poland challenge

Last week I have experimented with coloring map of Poland in national colors. Vaidotas Zemlys improved on my effort by adding colors to map of Lithuania and posted a challenge to also add coat of arms to the plot. This proved to be a nice exercise of using pixmap and grImport packages.

I wanted to compare adding coat of arms of Poland as bitmap and as vector graphics to the plot. First task was to get the picture. It can be found as SVG file. Code for plotting of the map was given in my last post so I show here only the code that needs to be added to plot coat of arms.

Bitmap graphics
To add bitmaps to R plots one can use pixmap package. First one needs to convert SVG to PPM format which can be done using GIMP. Then I added the following code:

library(pixmap)
coat <- read.pnm("Herb_Polski.ppm")
usr <- par("usr")
pin <- par("pin")
x.asp <- (coat@size[2] * (usr[2] - usr[1]) / pin[1])
y.asp <- (coat@size[1] * (usr[4] - usr[3]) / pin[2])
coat.height <- 0.7 * (poland$range[4] - mid)
coat.width <- coat.height * x.asp / y.asp
y.0 <- mid + coat.height / 10
x.0 <- mean(poland$range[1:2]) - coat.width / 2
bbox <- c(x.0, y.0, x.0 + coat.width, y.0 + coat.height)
coat <- read.pnm("Herb_Polski.ppm",
                 bbox = bbox)
plot(coat, add = TRUE)

The key problem was to get proper scaling of the picture. Here is the result:


Vector graphics
For vector graphics package grImport is an option. This time SVG has to be converted to PS. I used Inscape to do it. The PS file can be added to the map using the following code:

library(grImport)
PostScriptTrace("Herb_Polski.ps")
herb <- readPicture("Herb_Polski.ps.xml")
grid.picture(herb, x=0.5, y=0.635, width = 0.25, height = 0.25)

Here an important thing to notice is the way picture is non-standard way of picture positioning on the plot. The result is:


Surprise
In this case picture obtained using bitmap looks a bit better than the one obtained using vector graphics. This was a surprise.

1 comment: