# sept-2019-dem-debate-final.R -rw-r--r-- 1.8 KiB View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
library(hrbrthemes)
library(ggchicklet)
library(stringi)
library(tidyverse)

debates_src <- jsonlite::fromJSON("https://int.nyt.com/newsgraphics/2019/debates/2019-09-12-dem-debate/data.json")

debates_src %>% 
  as_tibble() %>% 
  mutate(
    speaker = stri_trans_totitle(speaker),
    timestamp = parse_time(timestamp),
    elapsed = as.numeric(elapsed) / 60,
    topic = stri_trans_totitle(topic)
  ) %>% 
  mutate(
    speaker = case_when(
      speaker == "Orourke" ~ "O'Rourke",
      TRUE ~ speaker
    )
  ) %>% 
  filter(
    !is.na(timestamp), speaker != "Moderator"
  ) %>% 
  mutate(
    speaker = fct_reorder(speaker, elapsed, sum, .desc=FALSE),
    topic = fct_inorder(topic)
  ) -> debates

count(debates, speaker, wt=elapsed, sort=TRUE) %>%
  mutate(speaker = fct_reorder(speaker, n, sum, .desc=FALSE)) %>%
  mutate(speaker = fct_inorder(speaker) %>% fct_rev()) 

debates %>%
  mutate(speaker = fct_reorder(speaker, elapsed, sum, .desc=FALSE)) %>%
  ggplot() +
  geom_chicklet(
    aes(speaker, elapsed, group = timestamp, fill = topic),
    width = 0.75
  ) +
  scale_x_discrete(expand = c(0,0)) +
  scale_y_continuous(
    expand = c(0, 0.0625),
    position = "right",
    breaks = seq(0, 14, 2),
    labels = c(0, sprintf("%d min.", seq(2, 14, 2)))
  ) +
  ggthemes::scale_fill_tableau("Tableau 20") +
  coord_flip() +
  labs(
    x = NULL, y = NULL, fill = NULL,
    title = "How Long Each Candidate Spoke",
    subtitle = "Septemnber 2019 Democratic Debates",
    caption = "Each bar segment represents the length of a candidate’s response to a question.\n\nOriginal <https://www.nytimes.com/interactive/2019/09/12/us/elections/debate-speaking-time.html>\n#rstats reproduction by @hrbrmstr"
  ) +
  theme_ipsum_rc(grid="X") +
  theme(axis.text.x = element_text(color = "gray60", size = 10)) +
  theme(legend.position = "top")