The following script queries Twitter and then produces a map of the results. It is adapted from https://opensource.com/article/17/6/collecting-and-mapping-twitter-data-using-r
Searching Twitter with R is a straightforward process, but there are some considerations that can make things more difficult.
The first step is to load the required packages. We will be using:
leafletwhich will help make the map by providing background tiles
mapsto help do the processing of the data
magrittris a package that facilitates data cleaning and munging
It is also important to define variables for key paramaters from the Twitter API. When a developer account is set up, several tokens and keys will be generated.
These should be considered secret and not included in code that is shared!
Replace the parameters in the script with the keys and tokens for your developer account.
#Install the required packages install.packages("twitteR") install.packages("leaflet") install.packages("maps") install.packages("magrittr") library(leaflet) library(maps) library (magrittr) require(twitteR) #Parameters for the request to Twitter reqURL <- "https://api.twitter.com/oauth/request_token" accessURL <- "https://api.twitter.com/oauth/access_token" accessToken <- "YOUR ACCESS TOKEN GOES HERE" accessTokenSecret <-"YOUR ACCESS TOKEN SECRET GOES HERE" authURL <- "https://api.twitter.com/oauth/authorize" apiKey <- "YOUR API KEY GOES HERE" apiSecret <- "YOUR API SECRET GOES HERE" CUSTOMER_KEY <-"YOUR CUSTOMER KEY GOES HERE" CUSTOMER_SECRET <-"YOUR CUSTOMER SECRET GOES HERE" setup_twitter_oauth(CUSTOMER_KEY, CUSTOMER_SECRET, accessToken, accessTokenSecret)
The next step is to query Twitter using the
searchTwitter function and then produce the map.
A full description of the searchTwitter function including the available arguments can be found at: https://www.rdocumentation.org/packages/twitteR/versions/1.1.9/topics/searchTwitter
# Search Twitter tweets <- searchTwitter("#AIDS OR #HIV", n = 500, lang = "en") #Put the results in a data frame tweets.df <-twListToDF(tweets)
There is a lot of information returned from the search including things such as how many followers the sender has, how frequently it was favorited and lat/long.
Before being able to map the tweets, it is necessary to convert them from string values to numeric.
Note: Only a small percentage of users include geotags in their tweets so there may be many results which can't be mapped.
# Change the lat/long values in the twitter results to numeric tweets.map <- transform(tweets.df, latitude=as.numeric(latitude), longitude=as.numeric(longitude)) # Eliminate the ones without a geocode tweets.map <- tweets.map[-which(is.na(tweets.map$longitude)),]
If you'd like you can output the results into a .csv file
#Uncomment the following line to write them into a CSV file for additional analysis #write.csv(tweets.df, file ="YOUR PATH HERE")
The next step is to use
leaflet to produce a map.
m <- leaflet(tweets.map) %>% addTiles() m %>% addCircles(lng = ~longitude, lat = ~latitude, popup = m$type, weight = 8, radius = 40, color = "#fb3004", stroke = TRUE, fillOpacity = 0.8)
Here is an image of a map showing one of the results from the search. The leaflet code can be adapted to produce an html version of the map that can be included in a website.