Mapping, R, Twitter

Mining and mapping geo-located tweet data

Sometimes you might want mine look at tweets posted within a certain geographical area rather than the entirety of the Twitter network. We will go through that and plot the tweets’ location a map.

Thankfully the Twitter API allows geo-coded searches.

First you will need to load the required packages and authenticate. Note that I use the github version of the twitteR package rather than the CRAN repository one – It is much easier to authenticate since it does not require that cacert.pem file.

libs <- c("twitteR", "ggmap", "ggplot2")
lapply(libs, library, character.only=TRUE)

Once this is done we can pull some geo-coded tweets. We basically build a data frame [,2] of the search terms and locations we want the tweets from. We precise the location by specifying precise coordinates and the radius in miles  – thereby defning an area.

  1. Define the search terms in a vector
  2. Define the location I want the post from
  3. Define the radius
  4. Combine longitude, latitude and radius in one element
  5. Form dataframe
search_terms <- c("lobster", "heaven")

#Coordinates of somewhere-in-Los-Angeles
location <- "Los Angeles"
longitude <- c("-117.979146")
latitude <- c("33.959581")
radius <- "80mi"
latlong <- paste(latitude,longitude,radius,sep=",")
latlong <- rep(ll, length(search_terms))
search_terms <- as.data.frame(cbind(latlong, search_terms))
search_terms$search_terms <- as.character(search_terms$search_terms)
search_terms$latlong <- as.character(search_terms$latlong)
search_terms$location <- location

Now we can start looking for tweets by looping through the dataframe.

tweets <- data.frame()

for (i in 1:nrow(search_terms)){
  print(paste("Looking for",search_terms$search_terms[i], "in", search_terms[i,]$location))
  tw = searchTwitter(search_terms[i,]$search_terms,n=round(2000/nrow(search_terms)), geocode=search_terms[i,]$latlong, lang="en")
  if (length(tw) == 0){
    print(paste("No tweets found for", search_terms$search_terms[i], "in", location))
  } else {
  tweets=rbind(twListToDF(tw), tweets)

That should get you a dataframe of tweets like this

 [1] "text"          "favorited"     "favoriteCount" "replyToSN"     "created"       "truncated"
 [7] "replyToSID"    "id"            "replyToUID"    "statusSource"  "screenName"    "retweetCount"
[13] "isRetweet"     "retweeted"     "longitude"     "latitude"

We count the number of tweets by longitude and latitude (location) and plot the points on the map.

df <- count(df, c("longitude","latitude"))
df <- na.omit(df)

map <- get_map(location=c(lon=-118.252431,lat=33.977804), zoom=10, maptype="hybrid")
finalmap <- ggmap(map, base_layer = ggplot(aes(x=longitude, y=latitude), data = df))

finalmap +
  stat_density2d(aes(x = longitude, y = latitude, fill = ..level.., alpha = ..level.., size=freq),
                 bins = 9, geom = "polygon",alpha=0.2,
                 data = df) +
  scale_fill_gradient(trans = "sqrt", low = "grey5", high = "greenyellow") +
  geom_point(col="grey80", size = (df$freq)/10)


And here we go – you can change the map type to any of these – “terrain”, “satellite”, “roadmap”, “hybrid”.




  1. Pingback: Sentiment Analysis in R | SocialFunction() - September 9, 2014

  2. Pingback: Tweet location and created time on Google Earth | SocialFunction() - September 18, 2014

  3. Pingback: Twitter crawler with R & [DBM] | SocialFunction() - December 17, 2014

  4. Pingback: Tweet location and created time on Google Earth - Jabber Cruncher - May 24, 2015

  5. Pingback: twitteR & SQL | Access - Jabber Cruncher - May 24, 2015

  6. Pingback: Sentiment Analysis in R - Jabber Cruncher - May 24, 2015


Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: