Skip to content

Instantly share code, notes, and snippets.

@MattSandy
Created June 19, 2018 04:09
Show Gist options
  • Save MattSandy/772aa809458dfa4e1eff250e60ed0bd0 to your computer and use it in GitHub Desktop.
Save MattSandy/772aa809458dfa4e1eff250e60ed0bd0 to your computer and use it in GitHub Desktop.
Tweets from 2015 and 2018
library("lubridate")
library("patchwork")
library("viridisLite")
library("ggplot2")
library("jsonlite")
# http://www.trumptwitterarchive.com/archive
df <- read_json("data.json",simplifyVector = T)
df$date <- as.POSIXct(df$created_at, format = "%a %b %d %H:%M:%S + 0000 %Y")
df <- df[which(!is.na(df$date)),]
df$Hour <- hour(df$date)
df$Year <- factor(year(df$date))
df$Day <- wday(df$date, label = T)
df$Witch <- grepl("Witch",df$text,ignore.case=T)
df <- df[which(df$Year %in% c(2015,2018)),]
df.2015 <- df[which(df$Year==2015),]
df.2018 <- df[which(df$Year==2018),]
# 2015
df.day.2015 <- table(Day = df.2015$Day,Witch = df.2015$Witch)
df.day.2015 <- data.frame(prop.table(df.day.2015, 1))
df.hour.2015 <- table(Hour = df.2015$Hour,Witch = df.2015$Witch)
df.hour.2015 <- data.frame(prop.table(df.hour.2015, 1))
# 2018
df.day.2018 <- table(Day = df.2018$Day,Witch = df.2018$Witch)
df.day.2018 <- data.frame(prop.table(df.day.2018, 1))
df.hour.2018 <- table(Hour = df.2018$Hour,Witch = df.2018$Witch)
df.hour.2018 <- data.frame(prop.table(df.hour.2018, 1))
p <- p1 <- ggplot() + theme_bw()
# 2015
p1 <- p + geom_bar(stat = "identity", data = df.day.2015) +
aes(x = Day, y = Freq, fill = Witch) +
scale_fill_viridis_d(option = "C") +
ggtitle("2015") +
ylab('Prop. of Tweets using "Witch"') +
guides(fill=FALSE)
p2 <- p + geom_bar(stat = "identity", data = df.hour.2015) +
aes(x = Hour, y = Freq, fill = Witch) + scale_fill_viridis_d(option = "C") +
ylab('Prop. of Tweets using "Witch"') +
guides(fill=FALSE)
p3 <- p + geom_bar(data = df.2015) + aes(Hour, fill = Day) + scale_fill_viridis_d()
# 2018
p4 <- p + geom_bar(stat = "identity", data = df.day.2018) +
aes(x = Day, y = Freq, fill = Witch) +
scale_fill_viridis_d(option = "C") +
ggtitle("2018") +
ylab('')
p5 <- p + geom_bar(stat = "identity", data = df.hour.2018) +
aes(x = Hour, y = Freq, fill = Witch) + scale_fill_viridis_d(option = "C") +
ylab('')
p6 <- p + geom_bar(data = df.2015) + aes(Hour, fill = Day) +
scale_fill_viridis_d() +
ylab('Number of Tweets') + guides(fill=FALSE)
p7 <- p + geom_bar(data = df.2018) + aes(Hour, fill = Day) +
scale_fill_viridis_d() +
ylab('Number of Tweets') + ylab('')
(((p1|p4) / (p2|p5)) / (p6|p7))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment