time-lining the Trump presidency

Introduction

The uspols package includes a simple function for scraping a Wikipedia-based timeline of the Trump presidencyuspols_wiki_timeline().

library(devtools)
devtools::install_github("jaytimm/uspols")
library(uspols) 

The function returns an up-to-date table of daily administration happenings from 20 Jan 2017 onward. Here we walk through a simple work-flow for adding timeline data to time series data. While not an official accounting, a super quick/easy way to get one’s bearings (in a maelstrom) and contextualize data points.

longs <- uspols::uspols_wiki_timeline()

Table structure is detailed some below. The folks at Wikipedia have delineated events (chronologically?) via bullet points per each day of Trump’s presidency, which have been enumerated here in the bullets column as independent rows. I have mostly done this to make text easier to read/access/etc. I am not sure how meaningful event distinctions (for a given day) actually are.

library(tidyverse)
longs %>%
  select(-quarter:-daypres, -dow) %>%
  head() %>%
  DT::datatable(rownames = FALSE, 
                options = list(dom = 't',
                               pageLength = 6,
                               scrollX = TRUE))

A simple use-case

summary <- longs %>%
  filter(!is.na(Events)) %>%
  mutate(tokens = tokenizers::count_words(Events)) %>%
  group_by(date) %>%
  mutate(daily_count = sum(tokens)) %>%
  slice(1) %>%
  ungroup()

For demonstration, we calculate total word counts of event descriptions per day from the time-line table. The plot below, then, summarizes these daily counts since inauguration. The plot itself is fairly meaningless, but the hover-action should be useful. For clarity purposes, only the first event for each day is included in the pop-up,.

dp <- summary %>%
  mutate(text = stringr::str_wrap(string = Events,
                                  width = 20,
                                  indent = 1,
                                  exdent = 1)) %>%
  
  plotly::plot_ly(x = ~date, 
                  y = ~daily_count,
                  text = ~text,
                  type = 'scatter',
                  mode = 'lines') %>%
  
  plotly::layout(#atitle = "Top 10 Drug Types", 
                 tooltip = c('Events'),
                 yaxis = list (title = "Daily event word count per Wikipedia"))

Example plot with Trump daily event on hover

Share