Load packages:
library(tidyverse)
## ── Attaching packages ──────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.1.0 ✔ purrr 0.3.0
## ✔ tibble 2.0.1 ✔ 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
Get 10000 of Trump’s tweets:
trump_tweets <- get_timeline("realDonaldTrump", n = 10000)
Which hastage does Trump use?
trump_tweets %>%
select(hashtags) %>%
unnest() %>%
mutate(hashtags = tolower(hashtags)) %>%
count(hashtags, sort = T) %>%
datatable()
A table of the number of tweets each day
trump_tweets %>%
group_by(Day = date(created_at)) %>%
summarise(tweets_per_day = n()) %>%
datatable()
The overall average number of tweets per day
trump_tweets %>%
group_by(Day = date(created_at)) %>%
summarise(tweets_per_day = n()) %>%
summarise(mean(tweets_per_day))
The number of tweets per day
trump_tweets %>%
group_by(Day = date(created_at)) %>%
plot_ly(x = ~Day) %>%
add_histogram()
A table of the hour of the day that Trump tweets
trump_tweets %>%
mutate(Time = hour(with_tz(created_at, "America/New_York"))) %>%
count(Time) %>%
datatable(options = (list(pageLength = 24)), rownames = F)
A histogram of the hour of the day that Trump tweets
trump_tweets %>%
mutate(Time = hour(with_tz(created_at, "America/New_York"))) %>%
plot_ly(x = ~Time) %>%
add_histogram() %>%
layout(title = "When Trump Tweets",
xaxis = list(title = "Time of Day (0 = midnight)"),
yaxis = list(title = "Number of Tweets"))
A table of the week days that Trump tweets
trump_tweets %>%
mutate(Day = wday(created_at, label = T)) %>%
count(Day) %>%
datatable(rownames = F)
A histogram of the week days that Trump tweets
trump_tweets %>%
mutate(Day = wday(created_at, label = T)) %>%
plot_ly(x = ~Day) %>%
add_histogram() %>%
layout(title = "When Trump Tweets",
xaxis = list(title = "Day of the week"),
yaxis = list(title = "Number of Tweets"))
A plotly heatmap of the weekday and time of day that Trump tweets
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_histogram2d() %>%
layout(title = "When Trump Tweets",
xaxis = list(title = "Day of the week"),
yaxis = list(title = "Time of Day (0 = midnight)"))
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",
xaxis = list(title = "Day of the week"),
yaxis = list(title = "Time of Day (0 = midnight)"))