R has a Rich Selection of Static and Interactive Map Tools (Demo)

R has always had the ability to perform GIS analysis, but for a long time the representation of data on maps was a relative weak suit of R. That’s changed. This tutorial quickly walks through the creation of maps in R using maps, googleVis, ggmap, RgoogleMaps, plotGoogleMaps and Inkscape/Adobe Illustrator.

The code to accompany this tutorial is available on Github (and feel free to follow my Github account!).

Someone recently asked how to map all 50 states (as opposed to only the US mainland) in R, which led me to review the different ways that this can be accomplished. As a starting point, even the old maps package for R can generate such a plot:

 ## File: RMapsDemo.R
 ## Description: Code file for demonstrating GIS/maps in R
 ## Copyright: (c) 2014, Hack-R.com (Jason D. Miller)

 # Import Libraries and Functions ------------------------------------------
 source("RMapsDemo_Functions.R")

 # Basic Maps --------------------------------------------------------------
 # Ye olde map package
 map("world", c("USA", "hawaii"), xlim = c(-180, -65), ylim = c(19, 72))
 

Rplot

Personally, I find Google to have the most attractive maps. Mathematician Markus Gesmann created a cool package called googleVis which – among many other very useful graphs – provides maps from Google which look like those that you’d see in a Google Adsense analytics dashboard.

 

 # googleVis ---------------------------------------------------------------
require(googleVis) 

GV <- gvisGeoChart(CityPopularity, locationvar='City', colorvar='Popularity',
                   options=list(region='US', height=350,
                              displayMode='markers',
                               colorAxis="{values:[200,400,600,800],
                               colors:[\'red', \'pink\', \'orange',\'green']}")) 

plot(GV)
 

GV

 

Not bad, eh? You can also easily change to satillite or hybrid views of your maps and best of all, they’re fully interactive with customizable data displayed on mouse hovers and customizable algorithms to define scaling and color schemes.

Still, this looks a little different than what you’re used to when you use Google Maps for driving directions or to find the nearest pub library. You have a few options if you’d prefer the latter, including ggmap, RGoogleMaps, and plotGoogleMaps.

There are lots of options which you can tweak, but for a simple example let’s create a close up map of some clubs in the financial district of Manhattan. We can put custom colored markers on top of the map to represent our potential club choices:

 lat = c(40.702147,40.718217,40.711614);
 lon = c(-74.012318,-74.015794,-73.998284);
 center = c(mean(lat), mean(lon));
 zoom <- min(MaxZoom(range(lat), range(lon)));

 markers = paste0("&markers=color:blue|label:S|40.714511,-74.009684&markers=color:", "green|label:G|40.714511,-74.009684&markers=color:red|color:red|")

ClubMap <- GetMap(center=center, zoom=zoom,markers=markers,destfile = "Dolls.png");

PlotOnStaticMap(ClubMap)
 

 

rgooglemaps

Another approach that will give you a more attractive result than maps is to follow the approach of this tutorial which shows how to import custom maps from Inkscape (or, equivalently, Adobe Illustrator) into R for plotting.

You’ll end up with something like this:

LKAAn