-->

Geocoding location data with dismo

Today’s Gist could actually end up being very useful to a number of you. It’s something of a trumped-up example, but it illustrates in very simple code how to do three interesting things:

  1. Gather Tweets by search term (which we’ve done before), and look up user info for each of the users returned by that search.
  2. Convert textual user location data to approximate latitude & longitude coordinates with the Google geocoding web-service, using a single function, geocode(), from the dismo package. This is a revelation to me, and though there appears to be a daily rate limit, I can imagine so many applications for which this would be useful.
  3. Very easily plot a world map (albeit with a lame projection), and superimpose points indicating the inferred location of #rstats-Tweeting users.

And all in just 29 (+/-) lines. Truly, truly, we are living in a great era for statistical computing.

Dot-density maps with spsample()

Today’s example is a little odd, in that the code isn’t pretty and the example isn’t really something you’d actually produce in real life — but if you’ll overlook those oddities, you’ll find that the spsample() function, and the sp package generally, can be very useful.

One of the problems with choropleth maps is the degree to which geographic areas can distort viewer perceptions of the distribution of the color-mapped variable. A good treatment of this problem can be found here, and cartograms are a typical solution. It is possible to retain the original map projection shape, but convey density with transparency or dot-density, and the latter is what today’s Gist illustrates.

Now, the odd thing about this particular example is that I illustrate the dot-density approach using state-level data — and it really doesn’t work as well as county-level data would. When county-level returns become available in a convenient format (let us know if you have it!), I’ll update this map. Until then, the principles are the same regardless of the shapefile you use.

This script goes through a couple of different steps:

  1. Scrape Wikipedia for election results (as seen here before)
  2. Load a state shapefile (as we’ve done previously)
  3. With a loop, convert each state shape to a SpatialPolygons object, and randomly draw points from within each state polygon to represent Democratic, Republican, and Other voters.
  4. Plot the map and these randomly drawn points, so that states with few votes appear very sparsely populated, while states with many votes appear densely populated. Coloring each point by candidate choice conveys the predominant leaning of each state.
As I mentioned, this would work better for counties, but in the meantime:

By d-sparks

Tags: rstats XML maps ggplot2 sp AdventCalendaR

Make your own electoral map

You’ve already seen everyone else’s electoral map (see this amazing array of maps from 2008), how would you like to make your own?

Today’s Gist allows you to do just that — input (manually!) state-by-state results, and output a beautiful choropleth map of presidential election results!

This code, of course, can be used for any number of state-level measures, so you might want to bookmark this one for future use.