library(tidyverse)
## ── Attaching packages ───────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.1.0 ✔ purrr 0.2.5
## ✔ tibble 2.0.0 ✔ dplyr 0.7.8
## ✔ tidyr 0.8.2 ✔ stringr 1.3.1
## ✔ readr 1.3.1 ✔ forcats 0.3.0
## ── Conflicts ──────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(DT)
library(plotly) # This package does interactive graphs
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(rtweet) # This package accesses Twitter data
##
## Attaching package: 'rtweet'
## The following object is masked from 'package:purrr':
##
## flatten
library(lubridate)
##
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
##
## date
token <- create_token(
app = "Research_psych_mc",
consumer_key = "Fzi8hs7FKkINEKJVd90af6GH6",
consumer_secret = "fgnL1l1M5P5tq3oumFj9G6y04NHrSFzm3w984fEXNXVWhCjVEv",
access_token = "222284483-KzmytYfsNVMTvYbmNuYiyotLJ9Brpffa2A9a6evw",
access_secret = "GAwhb9cDukKkFGyP9kVOqan0Wv8YbONdSuPB5PboNvpfY")
get_token() # this shows the token. make sure key is the same as consumer_key above
## <Token>
## <oauth_endpoint>
## request: https://api.twitter.com/oauth/request_token
## authorize: https://api.twitter.com/oauth/authenticate
## access: https://api.twitter.com/oauth/access_token
## <oauth_app> Research_psych_mc
## key: Fzi8hs7FKkINEKJVd90af6GH6
## secret: <hidden>
## <credentials> oauth_token, oauth_token_secret
## ---
Get a sample of 10,000 tweets by Donald Trump
trump_tweets <- get_timeline("realDonaldTrump", n = 10000)
trump_tweets %>%
select(hashtags) %>% # Focus on the hashtags
unnest() %>% # Separate multiple hashtags
mutate(hashtags = tolower(hashtags)) %>% # make all hashtags lowercase
count(hashtags, sort=TRUE) %>% # count how often they appear
datatable() # create an interactive table
trump_tweets %>%
group_by(Day = date(created_at)) %>% # extract the date, group by it
summarize(tweets_per_day = n()) %>%
datatable()
# count the number of tweets each day
Mean(tweets per day) by Trump
trump_tweets %>%
group_by(Day = date(created_at)) %>% # extract the date, group by it
summarize(tweets_per_day = n()) %>%
summarize(mean(tweets_per_day))
Histogram of tweets by Trump- number of tweets per day Hover mouse over various parts of graph to obtain more detailed information on timeline of tweets as they fluctuate.
trump_tweets %>%
mutate(day = date(created_at)) %>%
plot_ly(x = ~day) %>%
add_histogram() %>%
layout(title = "When Does @realDonaldTrump?tweet",
xaxis = list(title = "Day"),
yaxis = list(title = "Number of Tweets"))
When does Trump find the time to tweet? Timeline is equal to 24 hrs
trump_tweets %>%
mutate(Time = hour(with_tz(created_at, "America/New_York"))) %>%
count(Time) %>%
datatable(options = (list(pageLength = 24)), rownames = F)
Histogram of when Trump likes to tweet. Causing controversery from 6 AM- 11 PM but mostly over breakfast.
trump_tweets %>%
mutate(Time = hour(with_tz(created_at, "America/New_York"))) %>%
plot_ly(x = ~Time) %>%
add_histogram() %>%
layout(title = "When does Trump find the time to tweet?" ,
xaxis = list(title = "Time of Day (0 = midnight)"))
Days of the weekday that Trump tweets
trump_tweets %>%
mutate(Day = wday(created_at, # find the weekday that the tweet was created
label = T)) %>% # use labels (Sun, Mon, etc) rather than number
count(Day) %>% # count the number of tweets each day
datatable(rownames = F)
trump_tweets %>%
mutate(day = date(created_at)) %>%
plot_ly(x = ~day) %>%
add_histogram() %>%
layout(title = "When @realDonaldTrump?tweet")
Contour graph of the time and day Trump tweets. Calm before incoming storm.
trump_tweets %>%
mutate(Time = hour(with_tz(created_at, "America/New_York"))) %>%
mutate(day = wday(created_at, label = T)) %>%
plot_ly(x = ~day, y = ~Time) %>%
add_histogram2dcontour() %>%
layout(title = "When Trump Tweets",
yaxis = list(title = "time of day (0 = midnight)"))