Skip to content

Instantly share code, notes, and snippets.

@kendravant
Forked from dsparks/geocoded_Tweets.R
Created April 14, 2013 06:23
Show Gist options
  • Save kendravant/5381669 to your computer and use it in GitHub Desktop.
Save kendravant/5381669 to your computer and use it in GitHub Desktop.
doInstall <- TRUE
toInstall <- c("twitteR", "dismo", "maps", "ggplot2")
if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
searchTerm <- "#rstats"
searchResults <- searchTwitter(searchTerm, n = 1000) # Gather Tweets
tweetFrame <- twListToDF(searchResults) # Convert to a nice dF
userInfo <- lookupUsers(tweetFrame$screenName) # Batch lookup of user info
userFrame <- twListToDF(userInfo) # Convert to a nice dF
locatedUsers <- !is.na(userFrame$location) # Keep only users with location info
locations <- geocode(userFrame$location[locatedUsers]) # Use amazing API to guess
# approximate lat/lon from textual location data.
with(locations, plot(lon, lat))
worldMap <- map_data("world") # Easiest way to grab a world map shapefile
zp1 <- ggplot(worldMap)
zp1 <- zp1 + geom_path(aes(x = long, y = lat, group = group), # Draw map
colour = gray(2/3), lwd = 1/3)
zp1 <- zp1 + geom_point(data = locations, # Add points indicating users
aes(x = lon, y = lat),
colour = "RED", alpha = 1/2, size = 1)
zp1 <- zp1 + coord_equal() # Better projections are left for a future post
zp1 <- zp1 + theme_minimal() # Drop background annotations
print(zp1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment