-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake.R
192 lines (164 loc) · 6.67 KB
/
make.R
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
##----------------------------------------------------------------------------##
## DATA COLLECTION ##
##----------------------------------------------------------------------------##
## load rtweet, dplyr, and tidyverse
library(rtweet)
library(dataviz)
suppressPackageStartupMessages(library(tidyverse))
## also need installed
#pkgs <- c("ggrepel", "purrr", "tfse", "hms")
## 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)
## file name to save as (timestamped)
save_as <- sprintf("data/twitter_cspan_lists%s.rds",
stringr::str_replace_all(Sys.time(), " |:", "-"))
## save file
saveRDS(cspan_data, save_as)
##----------------------------------------------------------------------------##
## #DATAVIZ ##
##----------------------------------------------------------------------------##
## load tidyverse
#suppressPackageStartupMessages(library(tidyverse))
## read timestamped followers count file
data <- readRDS("data/timestamped-followers-data.rds")
## merge with running data set
cspan_data <- bind_rows(data,
select(cspan_data, user_id, screen_name,
followers_count, timestamp, cspan_list))
## save minimal form for public data
saveRDS(cspan_data, "data/timestamped-followers-data.rds")
## fast unique
funique <- function(x) {
## identify date-time, date, and character columns
psx <- vapply(x, inherits, "POSIXct",
FUN.VALUE = logical(1), USE.NAMES = FALSE)
## convert those columns to factors
x[psx] <- lapply(x[psx], as.integer)
## return only unique rows
x <- unique(x)
## convert columns back to date-time, date, and character
x[psx] <- lapply(x[psx], as.POSIXct, origin = "1970-01-01")
## return data
x
}
## round by hours
round2hour <- function(x) {
n <- 60 * 60
as.POSIXct(hms::hms(as.numeric(x) %/% n * n))
}
## save trimmed down CSV version for public data
#cspan_data %>%
# select(screen_name, followers_count, timestamp) %>%
# mutate(timestamp = round2hour(timestamp),
# timestamp = as.integer(timestamp)) %>%
# readr::write_csv("data/timestamped-followers-data.csv")
## 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
## 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
p <- cspan_data %>%
filter(followers_count > 6e5) %>%
congress_data() %>%
mutate(followers_count = sqrt(followers_count)) %>%
group_by(screen_name) %>%
mutate(mean = mean(followers_count)) %>%
ungroup() %>%
arrange(rev(timestamp)) %>%
mutate(lab = ifelse(duplicated(screen_name), NA, screen_name)) %>%
ggplot(aes(x = timestamp, y = followers_count,
colour = screen_name, label = lab)) +
theme_mwk(base_family = "Roboto Condensed") +
theme(legend.position = "none",
axis.title = element_text(hjust = .95, face = "italic")) +
geom_line(size = .6) +
ggrepel::geom_label_repel(family = "Roboto Condensed", size = 2.5,
label.size = .05, label.padding = .05, fill = "#ffffffbb") +
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 = "\u221a\u02c9Followers",
caption = "\nSource: Data collected via Twitter's REST API using rtweet (http://rtweet.info)")
pp <- p +
ggsave("plots/members-of-congress.png",
width = 9, height = 9, units = "in", device = "png")
## cabinet members
p <- cspan_data %>%
filter(followers_count > 6e5) %>%
cabinet_data() %>%
mutate(followers_count = sqrt(followers_count)) %>%
group_by(screen_name) %>%
mutate(mean = mean(followers_count)) %>%
ungroup() %>%
arrange(rev(timestamp)) %>%
mutate(lab = ifelse(duplicated(screen_name), NA, screen_name)) %>%
ggplot(aes(x = timestamp, y = followers_count,
colour = screen_name, label = lab)) +
theme_mwk(base_family = "Roboto Condensed") +
theme(legend.position = "none",
axis.title = element_text(hjust = .95, face = "italic")) +
geom_line(size = .6) +
ggrepel::geom_label_repel(family = "Roboto Condensed", size = 2.5,
label.size = .05, label.padding = .05, fill = "#ffffffbb") +
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 = "\u221a\u02c9Followers",
caption = "\nSource: Data collected via Twitter's REST API using rtweet (http://rtweet.info)")
pp <- p +
ggsave("plots/the-cabinet.png",
width = 9, height = 9, units = "in", device = "png")
## governors
p <- cspan_data %>%
filter(followers_count > 100000) %>%
governors_data() %>%
mutate(followers_count = sqrt(followers_count)) %>%
group_by(screen_name) %>%
mutate(mean = mean(followers_count)) %>%
ungroup() %>%
arrange(rev(timestamp)) %>%
mutate(lab = ifelse(duplicated(screen_name), NA, screen_name)) %>%
ggplot(aes(x = timestamp, y = followers_count,
colour = screen_name, label = lab)) +
theme_mwk(base_family = "Roboto Condensed") +
theme(legend.position = "none",
axis.title = element_text(hjust = .95, face = "italic")) +
geom_line(size = .6) +
ggrepel::geom_label_repel(family = "Roboto Condensed", size = 2.5,
label.size = .05, label.padding = .05, fill = "#ffffffbb") +
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 = "\u221a\u02c9Followers",
caption = "\nSource: Data collected via Twitter's REST API using rtweet (http://rtweet.info)")
pp <- p +
ggsave("plots/governors.png", width = 9,
height = 9, units = "in", device = "png")
## add, commit, and push to git
git2r::add(path = ".")
git2r::commit(message = "auto update")
system("git push")