# Exercise in grImport

January 13, 2012
By

(This article was first published on R snippets, and kindly contributed to R-bloggers)

Last week I used grImport for the first time. I decided to try perform another exercise using it. The task was to add voivodeship division of Poland.

Standard R maps do not contain such a division. I have found it on r-forge in package  mapoland based on ESRI shape files, but I wanted to import such a map from SVG file which can be found on Wikipedia. As last time SVG file has to be converted do PS first. Here is a comparison of both maps showing that the import worked quite well.

And this is the code I have used:

library(grImport)
PostScriptTrace(“Wojewodztwa.ps”)
voiv <- readPicture(“Wojewodztwa.ps.xml”)
broken.voiv <- explodePaths(voiv)
#extracting voivodeship ‘PictureStrokes’
xpath <- ypath <- list()
sel <- c(2,4,6,8,20,22,24,28,30,32,34,36,38,41,44,46)
for (i in seq(along = sel)) {
xpath[[i]] <- broken.voiv[[sel[i]]]@paths\$[email protected]
ypath[[i]] <- broken.voiv[[sel[i]]]@paths\$[email protected]
}
#adding Wolin island to zachodniopomorskie
xpath[[2]] <- c(broken.voiv[10]@paths\$[email protected][c(39:1, 95:40)],
xpath[[2]])
ypath[[2]] <- c(broken.voiv[10]@paths\$[email protected][c(39:1, 95:40)],
ypath[[2]])
library(mapoland)
pl <- getShape(“voiv”)
#functions rescaling paths to mapoland map size
transx <- function(x) {
old <- c(min(sapply(xpath,min)), max(sapply(xpath,max)))
new <- [email protected][1,]
((x old[1]) / (old[2] – old[1])) * (new[2] new[1])
+ new[1]
}
transy <- function(y) {
old <- c(min(sapply(ypath, min)), max(sapply(ypath, max)))
new <- [email protected][2,]
((y old[1]) / (old[2] – old[1])) * (new[2] new[1])
+ new[1]
}
plot(pl, lwd = 4)
for (i in seq(along = sel)) {
lines(transx(xpath[[i]]), transy(ypath[[i]]), col = “red”)
}

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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

## Sponsors

Contact us if you wish to help support R-bloggers, and place your banner here.

# Never miss an update! Subscribe to R-bloggers to receive e-mails with the latest R posts.(You will not see this message again.)

Click here to close (This popup will not appear again)