Posts Tagged ‘viz’

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

 

All Hail the Data Science Venn Diagram

Forged by the Gods, the ancient data science venn diagram is the oldest, most sacred representation of the field of data science.

 

Data_Science_Venn_DiagramI’ve been in love with this simple diagram since I first began working as a data scientist. I love it because it so clearly and simply represents the unique skillset that makes up data science. I’ll write more on this topic and how my own otherwise eclectic skillset coalesced into the practice of professional data science.

I wish I could take credit for creating this simple-but-totally-unsurpassed graphic. Over the past couple of years I’ve often used it as an avatar and if you look close enough you’ll even find it in the background of my (hacked) WordPress header image. While I like to think that it was immaculately convinced, the word on the street is that it was created for the public domain by Drew Conway, who is the co-author of Machine Learning for Hackers*, a private market intelligence and business consultant, my fellow recovering social scientist,  recent PhD grad from NYU, and a fast-rising name in data science (yea, he wants to be like me).

 
 
 
 
 
*It’s an O’reilly book on ML in R which I kept with me at all times for at least a year; the code is on GitHub and I highly recommend it, though it’s a little basic and its social network analysis section is based on the deprecated Google Social Graph API