Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
77 lines (57 sloc) 2.54 KB
#Twitter data basics
#3.14.18
#install other packages
install.packages('rtweet')
install.packages('tidyverse')
install.packages('maps')
install.packages('tidytext')
install.packages('httpuv')
install.packages('wordcloud')
#load libraries
library(rtweet)
library(tidyverse)
library(maps)
library(tidytext)
library(httpuv)
library(wordcloud)
#first establish authentication. Replace the dummy values below with your own
#if you forgot to set up a Twitter app or don't know how to get these values see the link where you downloaded this script
create_token(
app = "my_twitter_research_app",
consumer_key = "XYznzPFOFZR2a39FwWKN1Jp41",
consumer_secret = "CtkGEWmSevZqJuKl6HHrBxbCybxI1xGLqrD5ynPd9jG0SoHZbD",
access_token = "9551451262-wK2EmA942kxZYIwa5LMKZoQA4Xc2uyIiEwu2YXL",
access_secret = "9vpiSGKg1fIPQtxc5d5ESiFlZQpfbknEN1f1m2xe5byw7")
##search tweets and save them************************
#search for 1000 tweets sent from the US mentioning Trump
tweets <- search_tweets("#trump", geocode = lookup_coords("usa"), n = 1000)
#you could also search a user's timeline (see sentiment analysis script for example using these data)
trump <- get_timelines("realdonaldtrump", n = 500)
#this process unlists the tweet objects and saves 2 .csvs
save_as_csv(tweets, "temp.csv")
#you can do something like this if you want to save tweet/user data in one file
tweet_info <- read_csv("temp.tweets.csv")
users_info <- read_csv("temp.users.csv")
tweets_users <- cbind(tweet_info,user_info)
write_csv(tweets_users, "trump_tweets.csv")
##where are people tweeting about Trump?*****************
#plot on a map
#create lat/lng variables using all available tweet and profile geo-location data
#note: you may not have much lat_lng data in your available set, depends on which users appear in your set
tweets <- lat_lng(tweets)
#plot state boundaries
par(mar = c(0, 0, 0, 0))
maps::map("state", lwd = .25)
#plot lat and lng points onto state map
with(tweets, points(lng, lat, pch = 20, cex = .75, col = rgb(0, .3, .7, .75)))
##produce wordcloud for most frequently occuring words for Trump**************************
#tokenize
tweets_tokens <- tweets %>% unnest_tokens(word, text)
#plot
wordcloud(tweets_tokens$word, min.freq=30)
#you can take out commonly occuring expressions that aren't of interest to make a cleaner word cloud using something like this
tweets_tokens_trim <- tweets_tokens %>% filter(word != 'https')
#replot
wordcloud(tweets_tokens_trim$word, min.freq=30)
#save hi-res
ggsave("trump_wordcloud.jpg")
You can’t perform that action at this time.