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
Access 10,000 of Trump’s Tweets
trump_tweets <- get_timeline("realDonaldTrump", n = 10000)
Create a table illustrating Trump’s hashtag usage.
trump_tweets %>%
select(hashtags)%>%
unnest() %>%
mutate(hashtags = tolower(hashtags)) %>%
count(hashtags, sort = T) %>%
datatable()
Create a table showing the Trump’s number of tweets per day.
trump_tweets %>%
group_by(Day = date(created_at))%>%
summarize(tweets_per_day = n()) %>%
datatable()
Find the average number of Trump’s Tweets per day.
trump_tweets %>%
group_by(Day = date(created_at))%>%
summarize(tweets_per_day = n()) %>%
summarize(mean(tweets_per_day))
Create a gaphic displaying Trump’s average number of tweets per day.
trump_tweets %>%
group_by(Day = date(created_at))%>%
plot_ly(x = ~Day) %>%
add_histogram()
Create a table illustrating the hours that Trump tweets
trump_tweets %>%
mutate(Time = hour(with_tz(created_at, "America/New_York")))%>%
count(Time) %>%
datatable(options = (list(pagelength = 24)), rowname = F)
Create a histogram showing the time of 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)"))
Create a table and a histogram respresenting the days of the week that Trump Tweets.
trump_tweets%>%
mutate(Day =wday(created_at, label = T))%>%
count(Day) %>%
datatable( rownames = F)
Histogram
trump_tweets%>%
mutate(Day =wday(created_at, label = T))%>%
plot_ly(x = ~Day)%>%
add_histogram()%>%
layout(title = "When Trump Tweets")
Create a heatmap illustrating the times of the week and days 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",
yaxis = list(title = "Time of Day (0 = midgnight)"))
As one can see from the heatmap, it appears that Trump tweets most often Wednsdays around 8-9 in the morning.