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.
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:
coat <- read.pnm(“Herb_Polski.ppm”)
usr <- par(“usr”)
pin <- par(“pin”)
x.asp <- ([email protected] * (usr – usr) / pin)
y.asp <- ([email protected] * (usr – usr) / pin)
coat.height <- 0.7 * (poland$range – 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:
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:
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:
In this case picture obtained using bitmap looks a bit better than the one obtained using vector graphics. This was a surprise.