This repository has been archived by the owner on Dec 30, 2023. It is now read-only.
forked from mkearney/rstudioconf_tweets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
410 lines (322 loc) · 13.5 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
---
title: "rstudio::conf tweets"
author: "A repository for tracking [tweets about rstudio::conf 2018](https://twitter.com/hashtag/rstudioconf?f=tweets&vertical=default&src=hash). Read more about the Rstudio conference at [rstudio.com/conference/](https://www.rstudio.com/conference/)."
output:
github_document:
toc: true
df_print: "kable"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, collapse = TRUE)
```
## Data
Two data collection methods are described in detail below. Hoewver, if you want to skip straight to the data, run the following code:
```{r, eval=FALSE}
## download status IDs file
download.file(
"https://github.com/mkearney/rstudioconf_tweets/blob/master/data/search-ids.rds?raw=true",
"rstudioconf_search-ids.rds"
)
## read status IDs fromdownloaded file
ids <- readRDS("rstudioconf_search-ids.rds")
## lookup data associated with status ids
rt <- rtweet::lookup_tweets(ids$status_id)
```
### rtweet
Whether you lookup the status IDs or search/stream new tweets, make sure you've installed the [rtweet](http://rtweet.info) package. The code below will install [if it's not already] and load rtweet.
```{r}
## install rtweet if not already
if (!requireNamespace("rtweet", quietly = TRUE)) {
install.packages("rtweet")
}
## load rtweet
library(rtweet)
```
## Twitter APIs
There are two easy [and free] ways to get lots of Twitter data, filtering by one or more keywords. Each method is described and demonstrated below.
### Stream
The first way is to stream the data (using Twitter's stream API). For example, in the code below, a stream is setup to run continuously from the moment its executed until the Saturday at midnight (to roughly coincide with the end of the conference).
```{r, eval=FALSE}
## set stream time
timeout <- as.numeric(
difftime(as.POSIXct("2018-02-04 00:00:00"),
Sys.time(), tz = "US/Pacific", "secs")
)
## search terms
rstudioconf <- c("rstudioconf", "rstudio::conf",
"rstudioconference", "rstudioconference18",
"rstudioconference2018", "rstudio18",
"rstudioconf18", "rstudioconf2018",
"rstudio::conf18", "rstudio::conf2018")
## name of file to save output
json_file <- file.path("data", "stream.json")
## stream the tweets and write to "data/stream.json"
stream_tweets(
q = paste(rstudioconf, collapse = ","),
timeout = timeout,
file_name = json_file,
parse = FALSE
)
## parse json data and convert to tibble
rt <- parse_stream(json_file)
```
### Search
The second easy way to gather Twitter data using one or more keywords is to search for the data (using Twitter's REST API). Unlike streaming, searching makes it possible to go back in time. Unfortunately, Twitter sets a rather restrictive cap–roughly nine days–on how far back you can go. Regardless, searching for tweets is often the preferred method. For example, the code below is setup in such a way that it can be executed once [or even several times] a day throughout the conference.
```{r}
## search terms
rstudioconf <- c("rstudioconf", "rstudio::conf",
"rstudioconference", "rstudioconference18",
"rstudioconference2018", "rstudio18",
"rstudioconf18", "rstudioconf2018",
"rstudio::conf18", "rstudio::conf2018")
## use since_id from previous search (if exists)
if (file.exists(file.path("data", "search.rds"))) {
since_id <- readRDS(file.path("data", "search.rds"))
since_id <- since_id$status_id[1]
} else {
since_id <- NULL
}
## search for up to 100,000 tweets mentionging rstudio::conf
rt <- search_tweets(
paste(rstudioconf, collapse = " OR "),
n = 1e5, verbose = FALSE,
since_id = since_id,
retryonratelimit = TRUE
)
## if there's already a search data file saved, then read it in,
## drop the duplicates, and then update the `rt` data object
if (file.exists(file.path("data", "search.rds"))) {
## bind rows (for tweets AND users data)
rt <- do_call_rbind(
list(rt, readRDS(file.path("data", "search.rds"))))
## determine whether each observation has a unique status ID
kp <- !duplicated(rt$status_id)
## only keep rows (observations) with unique status IDs
users <- users_data(rt)[kp, ]
## the rows of users should correspond with the tweets
rt <- rt[kp, ]
## restore as users attribute
attr(rt, "users") <- users
}
## save the data
saveRDS(rt, file.path("data", "search.rds"))
## save shareable data (only status_ids)
saveRDS(rt[, "status_id"], file.path("data", "search-ids.rds"))
```
## Explore
To explore the Twitter data, go ahead and load the [tidyverse](http://tidyverse.org) packages.
```{r}
suppressPackageStartupMessages(library(tidyverse))
```
### Tweet frequency over time
In the code below, the data is summarized into a time series-like data frame and then plotted in order depict the frequency of tweets–aggregated using 2-hour intevals–about rstudio::conf over time.
```{r timefreq, fig.show='hide', fig.height=7, fig.width=9}
rt %>%
filter(created_at > "2018-01-29") %>%
ts_plot("2 hours", color = "transparent") +
geom_smooth(method = "loess", se = FALSE, span = .1,
size = 2, colour = "#0066aa") +
geom_point(size = 5,
shape = 21, fill = "#ADFF2F99", colour = "#000000dd") +
theme_minimal(base_size = 15, base_family = "Roboto Condensed") +
theme(axis.text = element_text(colour = "#222222"),
plot.title = element_text(size = rel(1.7), face = "bold"),
plot.subtitle = element_text(size = rel(1.3)),
plot.caption = element_text(colour = "#444444")) +
labs(title = "Frequency of tweets about rstudio::conf over time",
subtitle = "Twitter status counts aggregated using two-hour intervals",
caption = "\n\nSource: Data gathered via Twitter's standard `search/tweets` API using rtweet",
x = NULL, y = NULL)
```
<p align="center"><img width="100%" height="auto" src="README_files/figure-markdown_github/timefreq-1.png" /></p>
### Positive/negative sentiment
Next, some sentiment analysis of the tweets so far.
```{r sentiment, fig.show='hide', fig.height=7, fig.width=9}
## clean up the text a bit (rm mentions and links)
rt$text2 <- gsub(
"^RT:?\\s{0,}|#|@\\S+|https?[[:graph:]]+", "", rt$text)
## convert to lower case
rt$text2 <- tolower(rt$text2)
## trim extra white space
rt$text2 <- gsub("^\\s{1,}|\\s{1,}$", "", rt$text2)
rt$text2 <- gsub("\\s{2,}", " ", rt$text2)
## estimate pos/neg sentiment for each tweet
rt$sentiment <- syuzhet::get_sentiment(rt$text2, "syuzhet")
## write function to round time into rounded var
round_time <- function(x, sec) {
as.POSIXct(hms::hms(as.numeric(x) %/% sec * sec))
}
## plot by specified time interval (1-hours)
rt %>%
mutate(time = round_time(created_at, 60 * 60)) %>%
group_by(time) %>%
summarise(sentiment = mean(sentiment, na.rm = TRUE)) %>%
mutate(valence = ifelse(sentiment > 0L, "Positive", "Negative")) %>%
ggplot(aes(x = time, y = sentiment)) +
geom_smooth(method = "loess", span = .1,
colour = "#aa11aadd", fill = "#bbbbbb11") +
geom_point(aes(fill = valence, colour = valence),
shape = 21, alpha = .6, size = 3.5) +
theme_minimal(base_size = 15, base_family = "Roboto Condensed") +
theme(legend.position = "none",
axis.text = element_text(colour = "#222222"),
plot.title = element_text(size = rel(1.7), face = "bold"),
plot.subtitle = element_text(size = rel(1.3)),
plot.caption = element_text(colour = "#444444")) +
scale_fill_manual(
values = c(Positive = "#2244ee", Negative = "#dd2222")) +
scale_colour_manual(
values = c(Positive = "#001155", Negative = "#550000")) +
labs(x = NULL, y = NULL,
title = "Sentiment (valence) of rstudio::conf tweets over time",
subtitle = "Mean sentiment of tweets aggregated in one-hour intervals",
caption = "\nSource: Data gathered using rtweet. Sentiment analysis done using syuzhet")
```
<p align="center"><img width="100%" height="auto" src="README_files/figure-markdown_github/sentiment-1.png" /></p>
### Semantic networks
The code below provides a quick and dirty visualization of the semantic network (connections via retweet, quote, mention, or reply) found in the data.
```{r network, fig.show='hide', fig.height=36, fig.width=36}
## unlist observations into long-form data frame
unlist_df <- function(...) {
dots <- lapply(list(...), unlist)
tibble::as_tibble(dots)
}
## iterate by row
row_dfs <- lapply(
seq_len(nrow(rt)), function(i)
unlist_df(from_screen_name = rt$screen_name[i],
reply = rt$reply_to_screen_name[i],
mention = rt$mentions_screen_name[i],
quote = rt$quoted_screen_name[i],
retweet = rt$retweet_screen_name[i])
)
## bind rows, gather (to long), convert to matrix, and filter out NAs
rdf <- dplyr::bind_rows(row_dfs)
rdf <- tidyr::gather(rdf, interaction_type, to_screen_name, -from_screen_name)
mat <- as.matrix(rdf[, -2])
mat <- mat[apply(mat, 1, function(i) !any(is.na(i))), ]
## get rid of self references
mat <- mat[mat[, 1] != mat[, 2], ]
## filter out users who don't appear in RHS at least 3 times
apps1 <- table(mat[, 1])
apps1 <- apps1[apps1 > 1L]
apps2 <- table(mat[, 2])
apps2 <- apps2[apps2 > 1L]
apps <- names(apps1)[names(apps1) %in% names(apps2)]
mat <- mat[mat[, 1] %in% apps & mat[, 2] %in% apps, ]
## create graph object
g <- igraph::graph_from_edgelist(mat)
## calculate size attribute (and transform to fit)
matcols <- factor(c(mat[, 1], mat[, 2]), levels = names(igraph::V(g)))
size <- table(screen_name = matcols)
size <- (log(size) + sqrt(size)) / 3
## reorder freq table
size <- size[match(names(size), names(igraph::V(g)))]
## plot network
par(mar = c(12, 6, 15, 6))
plot(g,
edge.size = .4,
curved = FALSE,
margin = -.05,
edge.arrow.size = 0,
edge.arrow.width = 0,
vertex.color = "#ADFF2F99",
vertex.size = size,
vertex.frame.color = "#003366",
vertex.label.color = "#003366",
vertex.label.cex = .8,
vertex.label.family = "Roboto Condensed",
edge.color = "#0066aa",
edge.width = .2,
main = "")
par(mar = c(9, 6, 9, 6))
title("Semantic network of users tweeting about rstudio::conf",
adj = 0, family = "Roboto Condensed", cex.main = 6.5)
mtext("Source: Data gathered using rtweet. Network analysis done using igraph",
side = 1, line = 0, adj = 1.0, cex = 3.8,
family = "Roboto Condensed", col = "#222222")
mtext("User connections by mentions, replies, retweets, and quotes",
side = 3, line = -4.25, adj = 0,
family = "Roboto Condensed", cex = 4.9)
```
<p align="center"><img width="100%" height="auto" src="README_files/figure-markdown_github/network-1.png" /></p>
Ideally, the network visualization would be an interactive, searchable graphic. Since it's not, I've printed out the node size values below.
```{r}
nodes <- as_tibble(sort(size, decreasing = TRUE))
nodes$rank <- seq_len(nrow(nodes))
nodes$screen_name <- paste0(
'<a href="https://twitter.com/', nodes$screen_name,
'">@', nodes$screen_name, '</a>')
dplyr::select(nodes, rank, screen_name, log_n = n)
```
### Tidyverse vs. Shiny
This code identifies tweets by topic, detecting mentions of the tidyverse [packages] and shiny. It then plots the frequency of those tweets over time.
```{r topics, fig.show='hide', fig.height=7, fig.width=9}
rt %>%
filter(created_at > "2018-02-01" & created_at < "2018-02-05") %>%
mutate(
text = tolower(text),
tidyverse = str_detect(
text, "dplyr|tidyeval|tidyverse|rlang|map|purrr|readr|tibble"),
shiny = str_detect(text, "shiny|dashboard|interactiv")
) %>%
select(created_at, tidyverse:shiny) %>%
gather(pkg, mention, -created_at) %>%
mutate(pkg = factor(pkg, labels = c("Shiny", "Tidyverse"))) %>%
filter(mention) %>%
group_by(pkg) %>%
ts_plot("2 hours") +
geom_point(shape = 21, size = 3, aes(fill = pkg)) +
theme_minimal(base_family = "Roboto Condensed") +
scale_x_datetime(timezone = "America/Los_Angelos") +
theme(legend.position = "bottom",
legend.title = element_blank(),
legend.text = element_text(size = rel(1.1)),
axis.text = element_text(colour = "#222222"),
plot.title = element_text(size = rel(1.7), face = "bold"),
plot.subtitle = element_text(size = rel(1.3)),
plot.caption = element_text(colour = "#444444")) +
scale_fill_manual(
values = c(Tidyverse = "#2244ee", Shiny = "#dd2222")) +
scale_colour_manual(
values = c(Tidyverse = "#001155", Shiny = "#550000")) +
labs(x = NULL, y = NULL,
title = "Frequency of tweets about Tidyverse and Shiny during rstudio::conf",
subtitle = "Tweet counts aggregated for each topic in two-hour intervals",
caption = "\nSource: Data gathered using rtweet. Made pretty by ggplot2.")
```
<p align="center"><img width="100%" height="auto" src="README_files/figure-markdown_github/topics-1.png" /></p>
### Word clouds
I didn't want to add a bunch more code, so here I'm sourcing the prep work/code I used to get word lists.
```{r}
source(file.path("R", "words.R"))
```
#### Shiny word cloud
This first word cloud depicts the most popular non-stopwords used in tweets about Shiny.
```{r shiny, fig.show='hide', fig.height=7, fig.width=7}
par(mar = c(0, 0, 0, 0))
wordcloud::wordcloud(
shiny$var, shiny$n, min.freq = 3,
random.order = FALSE,
random.color = FALSE,
colors = gg_cols(5)
)
```
<p align="center"><img width="100%" height="auto" src="README_files/figure-markdown_github/shiny-1.png" /></p>
#### Tidyverse word cloud
The second word cloud depicts the most popular non-stopwords used in tweets about the tidyverse.
```{r tidyverse, fig.show='hide', fig.height=7, fig.width=7}
par(mar = c(0, 0, 0, 0))
wordcloud::wordcloud(
tidyverse$var, tidyverse$n, min.freq = 5,
random.order = FALSE,
random.color = FALSE,
colors = gg_cols(5)
)
```
<p align="center"><img width="100%" height="auto" src="README_files/figure-markdown_github/tidyverse-1.png" /></p>