Locate the position of CRAN mirror sites on a map using Google Maps

[This article was first published on One R Tip A Day, 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.

Inspired by this post (suggested here by the always useful Revolutions blog), I attempted to plot the position of CRAN mirrors on a map taking advantage of the nice R package RgoogleMaps (check the dependencies!). Below the code:

library(XML) # download.file("http://www.maths.lancs.ac.uk/~rowlings/R/Cranography/cran.gml",destfile="cran.gml") cran.gml <- xmlInternalTreeParse("cran.gml") # Create a data.frame assembling all the information from the gml file Name <- sapply(getNodeSet(cran.gml, "//ogr:Name"), xmlValue) Country <- sapply(getNodeSet(cran.gml, "//ogr:Country"), xmlValue) City <- sapply(getNodeSet(cran.gml, "//ogr:City"), xmlValue) URL <- sapply(getNodeSet(cran.gml, "//ogr:URL"), xmlValue) Host <- sapply(getNodeSet(cran.gml, "//ogr:Host"), xmlValue) Maintainer <- sapply(getNodeSet(cran.gml, "//ogr:Maintainer"), xmlValue) CountryCode <- sapply(getNodeSet(cran.gml, "//ogr:countryCode"), xmlValue) lng <- as.numeric(sapply(getNodeSet(cran.gml, "//ogr:lng"), xmlValue)) lat <- as.numeric(sapply(getNodeSet(cran.gml, "//ogr:lat"), xmlValue)) cran.mirrors <- data.frame(Name, Country, City, URL, Host, Maintainer, CountryCode, lng, lat) # cran.mirrors <- cbind(getCRANmirrors(), lng, lat) ## alternatively library(RgoogleMaps) # Define the markers: cran.markers <- cbind.data.frame( lat=cran.mirrors$lat, lon=cran.mirrors$lng, size=rep('tiny', length(cran.mirrors$lat)), col=colors()[1:length(cran.mirrors$lat)], char=rep('',length(cran.mirrors$lat)) ) # Get the bounding box: bb <- qbbox(lat = cran.markers[,"lat"], lon = cran.markers[,"lon"]) num.mirrors <- 1:dim(cran.markers)[1] ## to visualize only a subset of the cran.mirrors maptype <- c("roadmap", "mobile", "satellite", "terrain", "hybrid", "mapmaker-roadmap", "mapmaker-hybrid")[1] # Download the map (either jpg or png): MyMap <- GetMap.bbox(bb$lonR, bb$latR, destfile = paste("Map_", maptype, ".png", sep=""), GRAYSCALE=F, maptype = maptype) # Plot: png(paste("CRANMirrorsMap_", maptype,".png", sep=""), 640, 640) tmp <- PlotOnStaticMap(MyMap,lat = cran.markers[num.mirrors,"lat"], lon = cran.markers[num.mirrors,"lon"], cex=1, pch="R",col=as.numeric(cran.mirrors$Country), add=F) dev.off()


## Hosts from Italy maptype <- c("roadmap", "mobile", "satellite", "terrain", "hybrid", "mapmaker-roadmap", "mapmaker-hybrid")[4] num.it <- row.names(cran.mirrors[cran.mirrors$CountryCode=="IT",]) # Get the bounding box: bb.it <- qbbox(lat = cran.markers[num.it,"lat"], lon = cran.markers[num.it,"lon"]) # Download the map (either jpg or png): ITMap <- GetMap.bbox(bb.it$lonR, bb.it$latR, destfile = paste("ITMap_", maptype, ".png", sep=""), GRAYSCALE=F, maptype = maptype) #ITMap <- GetMap.bbox(bb.it$lonR, bb.it$latR, destfile = paste("ITMap_", maptype, ".jpg", sep=""), GRAYSCALE=F, maptype = maptype) # Plot: png(paste("CRANMirrorsMapIT_", maptype,".png", sep=""), 640, 640); tmp <- PlotOnStaticMap(ITMap,lat = cran.markers[num.it,"lat"], lon = cran.markers[num.it,"lon"], cex=2, pch="R",col="dodgerblue", add=F) # tmp <- PlotOnStaticMap(ITMap,lat = cran.markers[num.it,"lat"], lon = cran.markers[num.it,"lon"],labels=as.character(cran.mirrors[cran.mirrors$CountryCode=="IT",]$Host),col="black", FUN=text, add=T) dev.off()


CAVEAT: To reproduce the example you need the gml file you can download from here , a  Google account and a Google Maps API key. Here you can sign up for a free API key.

To leave a comment for the author, please follow the link and comment on their blog: One R Tip A Day.

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.

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)