-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
162 lines (131 loc) · 5.28 KB
/
README.Rmd
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
---
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, collapse = TRUE, comment = "#>")
```
## cspan_data
Tracking users-level data of (a) [Members of Congress](https://twitter.com/cspan/lists/members-of-congress),
(b) [The Cabinet](https://twitter.com/cspan/lists/the-cabinet/), and (c) [Governors](https://twitter.com/cspan/lists/governors)
using CSPAN Twitter lists and the [rtweet package](http://rtweet.info).
## \#dataviz
### Members of Congress
<p align="center"><img width="75%" height="auto" src="plots/members-of-congress.png" /></p>
### The Cabinet
<p align="center"><img width="75%" height="auto" src="plots/the-cabinet.png" /></p>
### Governors
<p align="center"><img width="75%" height="auto" src="plots/governors.png" /></p>
## Data collection script
Data collected using [rtweet](http://rtweet.info)
```{r, eval=FALSE}
## load rtweet and tidyverse
library(rtweet)
## define function for getting CSPAN Twitter lists data
get_cspan_list <- function(slug) {
## get users data of list members
x <- lists_members(slug = slug, owner_user = "CSPAN")
## document slug
x$cspan_list <- slug
## timestamp observations
x$timestamp <- Sys.time()
## return data
x
}
## cspan lists
cspan_lists <- c("members-of-congress", "the-cabinet", "governors")
## members of congress
cspan_data <- purrr::map(cspan_lists, get_cspan_list)
## merge into single data frame
cspan_data <- dplyr::bind_rows(cspan_data)
````
## Data visualization script
Plots created using [ggplot2](http://ggplot2.org/) and [ggrepel](https://github.com/slowkow/ggrepel)
```{r, eval=FALSE}
## load tidyverse
suppressPackageStartupMessages(library(tidyverse))
## read all files
data_files <- list.files("data", full.names = TRUE)
cspan_data <- map(data_files, readRDS)
## merge into single data set
cspan_data <- bind_rows(cspan_data)
## shortcuts for subsetting into data sets
congress_data <- function(cspan_data) filter(
cspan_data, cspan_list == "members-of-congress")
cabinet_data <- function(cspan_data) filter(
cspan_data, cspan_list == "the-cabinet")
governors_data <- function(cspan_data) filter(
cspan_data, cspan_list == "governors")
## plot most popular congress accounts
library(ggrepel)
## hacky function for labels
timestamp_range <- function(timestamp) {
n <- length(unique(timestamp))
x <- seq(min(timestamp), max(timestamp), length.out = (length(timestamp) / n))
nas <- rep(as.POSIXct(NA_character_), length(x))
c(x, rep(nas, n - 1L))
}
## member of congress
cspan_data %>%
filter(followers_count > 3e5) %>%
congress_data() %>%
mutate(followers_count = log10(followers_count)) %>%
arrange(timestamp) %>%
mutate(x = timestamp_range(timestamp)) %>%
group_by(screen_name) %>%
mutate(mean = mean(followers_count)) %>%
ungroup() %>%
ggplot(aes(x = timestamp, y = followers_count, colour = screen_name, label = screen_name)) +
theme_mwk(base_family = "Roboto Condensed") +
theme(legend.position = "none") +
geom_line() +
# geom_point() +
geom_label_repel(aes(x = x, y = mean), family = "Roboto Condensed") +
labs(title = "Tracking follower counts for members of Congress on Twitter",
subtitle = "Tracking the number of Twitter followers of members of the Congress over time",
x = NULL, y = "Number of followers (logged)",
caption = "\nSource: Data collected via Twitter's REST API using rtweet (http://rtweet.info") +
ggsave("plots/members-of-congress.png", width = 7, height = 13, units = "in")
## cabinet members
cspan_data %>%
cabinet_data() %>%
mutate(followers_count = log10(followers_count)) %>%
arrange(timestamp) %>%
mutate(x = timestamp_range(timestamp)) %>%
group_by(screen_name) %>%
mutate(mean = mean(followers_count)) %>%
ungroup() %>%
ggplot(aes(x = timestamp, y = followers_count, colour = screen_name, label = screen_name)) +
theme_mwk(base_family = "Roboto Condensed") +
theme(legend.position = "none") +
geom_line() +
# geom_point() +
geom_label_repel(aes(x = x, y = mean), family = "Roboto Condensed") +
labs(title = "Tracking follower counts for Cabinet members on Twitter",
subtitle = "Tracking the number of Twitter followers of members of the Cabinet over time",
x = NULL, y = "Number of followers (logged)",
caption = "\nSource: Data collected via Twitter's REST API using rtweet (http://rtweet.info") +
ggsave("plots/the-cabinet.png", width = 7, height = 13, units = "in")
## governors
cspan_data %>%
governors_data() %>%
mutate(followers_count = log10(followers_count)) %>%
arrange(timestamp) %>%
mutate(x = timestamp_range(timestamp)) %>%
group_by(screen_name) %>%
mutate(mean = mean(followers_count)) %>%
ungroup() %>%
ggplot(aes(x = timestamp, y = followers_count, colour = screen_name, label = screen_name)) +
theme_mwk(base_family = "Roboto Condensed") +
theme(legend.position = "none") +
geom_line() +
# geom_point() +
geom_label_repel(aes(x = x, y = mean), family = "Roboto Condensed") +
labs(title = "Tracking follower counts for U.S. Governors on Twitter",
subtitle = "Tracking the number of Twitter followers of Governors over time",
x = NULL, y = "Number of followers (logged)",
caption = "\nSource: Data collected via Twitter's REST API using rtweet (http://rtweet.info)") +
ggsave("plots/governors.png", width = 7, height = 9, units = "in")
```