Skip to content

Instantly share code, notes, and snippets.

@yonicd
Created January 20, 2019 13:46
Show Gist options
  • Save yonicd/ff721b3710c92a5e0631586a88c8cb6d to your computer and use it in GitHub Desktop.
Save yonicd/ff721b3710c92a5e0631586a88c8cb6d to your computer and use it in GitHub Desktop.
toddler in chief analysis
library(rvest)
library(patchwork)
tweet_threading <- function(tw){
last_found <- FALSE
while(!last_found){
nr <- nrow(tw)
last_found <- is.na(tw$reply_to_status_id[nr])
tw_tail <- rtweet::lookup_statuses(tw$reply_to_status_id[nr])
tw <- dplyr::bind_rows(tw,tw_tail)
}
tw
}
toc_head <- rtweet::lookup_statuses('1086605198167105537')
toc <- toc_head%>%tweet_threading()
plot_dat <- toc%>%
dplyr::mutate(
date = as.Date(created_at),
ym = strftime(date,'%Y-%m')
)%>%
dplyr::count(ym)%>%
dplyr::mutate(nn=cumsum(n))
p1 <- plot_dat%>%
ggplot2::ggplot(ggplot2::aes(x=ym,y=n)) +
ggplot2::geom_point(ggplot2::aes(size=n,colour=n)) +
ggplot2::theme_bw() +
ggplot2::theme(
axis.text.x = ggplot2::element_text(angle=90),
legend.position = 'bottom'
) +
ggplot2::labs(
x = "Date",
y = 'Statuses\n(Frequency)',
size = 'Count',
caption = '@yoniceedee'
) +
viridis::scale_color_viridis(option = 'B',end = 0.8,guide = 'none')
p2 <- plot_dat%>%
dplyr::mutate(i = 1:dplyr::n())%>%
ggplot2::ggplot(ggplot2::aes(x=i,y=nn)) +
ggplot2::geom_path() +
ggplot2::scale_x_continuous(breaks = 1:nrow(plot_dat),labels = plot_dat$ym) +
ggplot2::theme_bw() +
ggplot2::theme(
axis.text.x = ggplot2::element_text(angle=90)
) +
ggplot2::labs(
title = '"Toddler in Chief" Thread Timeline',
subtitle = sprintf('@dandrezner thread last updated at: %s',toc$created_at[1]),
x = "Date",
y = 'Statuses\n(Cumulative Frequency)'
)
p2 / p1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment