An R function to map your Twitter Followers

Tweet about this on TwitterShare on Facebook0Share on Google+0Share on LinkedIn0Email this to someone

I wrote a little function to make a personalized map of who follows you or who you follow on Twitter. The idea for this function was inspired by some plots I discussed in a previous post. I also found a lot of really useful code over at flowing data here

The function uses the packages twitteR, maps, geosphere, and RColorBrewer. If you don’t have the packages installed, when you source the twitterMap code, it will try to install them for you. The code also requires you to have a working internet connection. 

One word of warning is that if you have a large number of followers or people you follow, you may be rate limited by Twitter and unable to make the plot.

To make your personalized twitter map, first source the function:

> source(“http://biostat.jhsph.edu/~jleek/code/twitterMap.R”)

The function has the following form: 

twitterMap <- function(userName,userLocation=NULL,fileName=”twitterMap.pdf”,nMax = 1000,plotType=c(“followers”,”both”,”following”))

with arguments:

  • userName - the twitter username you want to plot
  • userLocation - an optional argument giving the location of the user, necessary when the location information you have provided Twitter isn’t sufficient for us to find latitude/longitude data
  • fileName - the file where you want the plot to appear
  • nMax - The maximum number of followers/following to get from Twitter, this is implemented to avoid rate limiting for people with large numbers of followers. 
  • plotType - if “both” both followers/following are plotted, etc. 

Then you can create a plot with both followers/following like so: 

> twitterMap(“simplystats”)

Here is what the resulting plot looks like for our Twitter Account:

If your location can’t be found or latitude longitude can’t be calculated, you may have to chose a bigger city near you. The list of cities used by twitterMap can be found like so:



>grep(“Baltimore”, world.cities[,1])

If your city is in the database, this will return the row number of the world.cities data frame corresponding to your city. 

If you like this function you may also like our function to determine if you are a data scientist or to analyze your Google Scholar citations page.
Update: The bulk of the heavy lifting done by these functions is performed by Jeff Gentry’s very nice twitteR package and code put together by Nathan Yau over at FlowingData. This is really an example of standing on the shoulders of giants. 
  • Hywel

    Thanks for the code.I used it fairly unthinkingly to produce a map of 1,000 followers of @IndigenousTweet http://bit.ly/1gSomWA. Your code used the data of the R maps package, the world.cities data especially. Of the 1,000 followers I looked at 304 didn't have a location shown in their profile. Of those 696 with locations, only 315 were
    geocoded. Some normalisation of the locations spelling etc may have improved the result, e.g. WASHINGTON DC, Washington D.C., and WASHINGTON, DC didn't get geocoded though Washington, DC did. Unfortunately though, the maps package data largely contain only English language placenames. Followers recording their location in other languages such those in Béal Feirste (Belfast), Baile Átha Cliath (Dublin), Donostia (San Sebastián) and Gasteiz (Vitoria) were thus not geocoded. Developers
    are often careless in assuring the authenticity of data sources and assuring the authenticity of geocoding is difficult. The maps package may be useful for teaching purposes but its data is flawed for this usage. Users beware!

    • jtleek

      Thanks Hywel, you are right the TwitterMap function is definitely klugey on the geo coding. This is due to (1) the fact that not all Twitter users report locations and (2) there are some tricky niceties (that you point out) to mapping locations.

      Of course I would be happy to have someone update the function and fix these issues, but I wrote it for fun in an hour or two on a weekend a few years ago so I'm not really maintaining it.

      • Hywel

        I agree, it's good fun. (I wish I could do as much in an hour or two!). I wasn't really suggesting you maintain your function. After all, given you've open sourced it, I could try improving the normalisation etc myself if I wanted. I just wanted to draw attention of others to the fact that it does not take account of the use of languages other than English.