Coat of arms of Poland challenge
[This article was first published on R snippets, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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.Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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.
To leave a comment for the author, please follow the link and comment on their blog: R snippets.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.