-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path01_gather-tweets.Rmd
More file actions
274 lines (187 loc) · 6.48 KB
/
01_gather-tweets.Rmd
File metadata and controls
274 lines (187 loc) · 6.48 KB
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
---
title: "gather with rtweet"
author: "John Little"
date: "`r Sys.Date()`"
output: html_notebook
---
license: "CC BY-NC"
Creative Commons: Attribution, Non-Commerical
https://creativecommons.org/licenses/by-nc/4.0/
Find this repository: https://github.com/libjohn/workshop_twitter_analysis
Much of this review comes from [Introduction to gathering tweets with rwteet](https://docs.ropensci.org/rtweet/articles/intro.html) using the [`rtweet` package](https://docs.ropensci.org/rtweet/). Conveniently, you **no longer need** a [Twitter API developer key](https://docs.ropensci.org/rtweet/articles/auth.html) to use this package. You **do need a Twitter account.**
<center>**How to gather twitter data via the API**</center>
## Load library packages
Use the [tidyverse](https://tidyverse.org) and rtweet
```{r message=FALSE, warning=FALSE}
library(tidyverse)
library(rtweet)
```
## Search tweets
`search_tweets()` - search a keyword(s) or hashtag
I recommend limiting the number of tweets returned (`n = 1000`) for this training. Otherwise you may hit a rate limit.
```{r}
#bts <- search_tweets("#BTS", n = 5000, include_rts = FALSE)
bts_dynamite <- search_tweets("#BTS dynamite", n = 1000, include_rts = FALSE)
```
### Access tokens: Problems with above?
If you were unable to authenticate with the Twitter API, you may be using an RStudio instance in the cloud. If that is a problem you will need to use the `rwteet::create_token()` function. Read more about obtaining and using tokens. **Specifically**, see under the authorization methods, section 2 [_Access token/secret method_](https://docs.ropensci.org/rtweet/articles/auth.html#access-tokensecret-method)
### Results
Gathering tweets will return a 90 variable tibble of whatever row-size you were able to collect. You'll want to spend some time familiarizing yourself with these variables and the range of data that can be gathered.
```{r}
# bts
bts_dynamite
```
```
{r}
glimpse(bts_dynamite)
```
### Your Turn
```
{r}
my_gathered_tweets <- search_tweets("_________", n = 1000, include_rts = FALSE)
```
## Serialize the gathering
If you're collecting data into the future, you may want to set your twitter API search to run on a schedule. How you set up your compute structure matters here. One way is to use the Duke VCM cloud computers along with the
- Windows: https://github.com/bnosac/taskscheduleR
- Linux: https://github.com/bnosac/cronR#cronr
## get friends
Find all the accounts a user follows
```{r}
john_little <- get_friends("john_little")
```
This returns the twitter name, i.e. `user`, and the `user_id` for each person following that user.
```{r}
john_little
```
Next, use `lookup_users` with the `user_id` to get more information about those accounts.
```{r}
john_little_data <- lookup_users(john_little$user_id)
```
```{r}
john_little_data
```
## get followers
Who is following me? `get_followers()`
```{r}
jrl_flw <- get_followers("john_little")
```
```{r}
jrl_flw_data <- lookup_users(jrl_flw$user_id)
```
```{r}
jrl_flw_data
```
## timelines
Get the most recent tweets from an account
```{r}
rg_tmls <- get_timelines("RhiannonGiddens", n = 3200)
```
```{r}
rg_tmls %>%
summarise(min(created_at), max(created_at))
```
### Visualize
```{r warning=FALSE}
rg_tmls %>%
dplyr::filter(created_at >= "2016-01-01") %>%
dplyr::group_by(screen_name) %>%
ts_plot("weeks", trim = 1L) +
ggplot2::geom_point() +
geom_smooth(se = FALSE, color = "cadetblue") +
colorblindr::scale_color_OkabeIto() +
hrbrthemes::theme_ipsum(grid = "Y") +
ggplot2::theme(
legend.title = ggplot2::element_blank(),
legend.position = "bottom",
plot.title = ggplot2::element_text(face = "bold")
) +
ggplot2::labs(
x = NULL, y = NULL,
title = "Frequency of Twitter statuses",
subtitle = "Twitter status (tweet) counts aggregated by week from Jan. 2016",
caption = "Source: Data collected from Twitter's REST API via rtweet"
)
# ggsave("images/giddens_timeline.png")
```
## get_favorites
Get the most recent favorites from a user
```{r}
rg_faves <- get_favorites("RhiannonGiddens", n = 3000)
```
```{r}
rg_faves
```
## Profiles
Search a users' profiles
```{r}
gullah <- search_users("#gullah", n = 1000)
```
```{r}
gullah
```
## get trends
What is trendingin a specific location?
```{r}
# sf <- get_trends("san franciso")
# durham <- get_trends(lat = 36.0, lng = -78.9)
greensboro <- get_trends("greensboro")
```
```{r}
greensboro
```
## Location information
Using the tidygeocoder R library, we can find location information when place_names are available.
### First, geocode
Use the [tidygeocoder](https://jessecambon.github.io/tidygeocoder) package.
```{r}
# glimpse(rg_tmls)
rg_places <- rg_tmls %>%
drop_na(place_name) %>%
select(place_name:bbox_coords) %>%
distinct() %>%
mutate(addr = glue::glue("{place_full_name}, {country}")) %>%
tidygeocoder::geocode(addr, method = "osm")
rg_places
```
### Visuzlize
You can create maps in R. Below is one of the easiest methods, especially if you know [ggplot2](https://ggplot2.tidyverse.org)
```{r}
rg_places %>%
distinct() %>%
drop_na(lat) %>%
ggplot(aes(long, lat), color="grey99") +
borders("world") +
geom_point(color = "goldenrod") +
ggrepel::geom_label_repel(aes(label = place_full_name),
segment.color = "goldenrod", segment.size = 1,
color = "navy",
max.overlaps = 20) +
theme_void()
# ggsave("images/giddens_locations_map.png")
```
### Second location example
Very similar to above. For accounts with "#gullah" in their profile, and that have location information listed, geocode the locations .....
```{r}
gullah_places <- gullah %>%
drop_na(place_name) %>%
select(place_name:bbox_coords) %>%
filter(country_code == "US") %>%
distinct() %>%
mutate(addr = glue::glue("{place_full_name}, {country}")) %>%
tidygeocoder::geocode(addr, method = "osm")
gullah_places
```
And now visualize on a US map of the _lower 48_ states.
You can learn more about basic R mapping from our workshop on [mapping with R](https://rfun.library.duke.edu/portfolio/mapping_workshop/)
```{r}
gullah_places %>%
distinct() %>%
drop_na(lat) %>%
ggplot(aes(long, lat), color="grey99") +
borders("state") +
geom_point(color = "goldenrod") +
ggrepel::geom_label_repel(aes(label = place_full_name),
segment.color = "goldenrod", segment.size = 1,
color = "navy") +
theme_void()
```