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
Get 1000 of Trump’s tweets
trump_tweets<- get_timeline("realDonaldTrump", n= 10000)
Most used hashtags
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())
# count the number of tweets each day
trump_tweets %>%
group_by(day = date(created_at)) %>% # extract the date, group by it
summarize(tweets_per_day = n()) %>%
summarize(mean(tweets_per_day))
# count the number of tweets each day
trump_tweets %>%
group_by(Day = date(created_at)) %>% # extract the date, group by it
plot_ly(x= ~Day) %>%
add_histogram()
# count the number of tweets each day
trump_tweets %>%
mutate(time = with_tz(created_at, "America/New_york")) %>%
mutate(time = hour(time)) %>%
count(time) %>%
datatable(options = (list(pageLength = 24)), rownames = F)
## Warning in with_tz(created_at, "America/New_york"): Unrecognized time zone
## 'America/New_york'
trump_tweets %>%
mutate(time = with_tz(created_at, "America/New_york")) %>%
mutate(time = hour(time)) %>%
plot_ly(x = ~time) %>% # create plotly graph
add_histogram() %>%
layout(title = "WhenTrump Tweets",
xaxis = list(title = "Time of Day (0 = Midnight)"))
## Warning in with_tz(created_at, "America/New_york"): Unrecognized time zone
## 'America/New_york'
trump_tweets %>%
mutate(Day = wday(created_at, # find the weekday that the tweet was created
label = T)) %>% # use labels (Sun, Mon, etc) rather than numbers
count(Day) %>% # count the number of tweets each day
datatable(rownames = F)
trump_tweets %>%
mutate(Day = wday(created_at, # find the weekday that the tweet was created
label = T)) %>%
plot_ly(x = ~ Day) %>% # create plotly graph
add_histogram() %>%
layout(title = "WhenTrump Tweets")
trump_tweets %>%
mutate(Day = wday(created_at, label = T)) %>%
mutate(Time = hour(with_tz(created_at, "America/New_york"))) %>%
plot_ly(x = ~ Day, y = ~ Time) %>%
add_histogram2d() %>%
layout(title= "When Trump Tweets",
yaxis = list(title= "Time of Day (0 = Midnight)"))
## Warning in with_tz(created_at, "America/New_york"): Unrecognized time zone
## 'America/New_york'
trump_tweets %>%
mutate(Day = wday(created_at, label = T)) %>%
mutate(Time = hour(with_tz(created_at, "America/New_york"))) %>%
plot_ly(x = ~ Day, y = ~ Time) %>%
add_histogram2dcontour() %>%
layout(title= "When Trump Tweets",
yaxis = list(title= "Time of Day (0 = Midnight)"))
## Warning in with_tz(created_at, "America/New_york"): Unrecognized time zone
## 'America/New_york'