public
Last active

Gathering Tweets, geocoding users, and plotting them

  • Download Gist
geocoded_Tweets.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
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)

Great example, thanks! I am, however, getting an error on
>searchResults <- searchTwitter(searchTerm, n = 1000)
Error in .self$twFromJSON(out) :
Error: Malformed response from server, was not JSON

After Googling, this may be due to non-Latin characters in some of the tweets. Do you have any suggestions on how to modify the above example so that it is robust to this problem?

Thanks,
Matt

I received the same error. Mine works as long as I run the package "RJSONIO".

Thank you! I will try that.

I had the same error. Installing RJSONIO did not seem to help.

you need to install AND load RJSONIO.

So make sure you do

library(RJSONIO)

RJSONIO did not help me either.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.