-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path01_count_tweets.R
155 lines (123 loc) · 3.68 KB
/
01_count_tweets.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
library(academictwitteR)
library(ggplot2)
library(dplyr)
library(lubridate)
library(gganimate)
library(ggthemes)
#simple hogmanay
tweetcounts <- count_all_tweets(
query = "#Jan25",
start_tweets = "2010-12-27T00:00:00Z",
end_tweets = "2022-06-01T00:00:00Z",
bearer_token = get_bearer(),
granularity = "day",
n = 10000
)
#reformat date
tweetcounts$time <-
parse_date_time(tweetcounts$start,
orders = "ymd HMS")
#plot
tweetcounts %>% ggplot() +
geom_line(aes(time, tweet_count)) +
ylab("#Jan25 tweet count")
saveRDS(tweetcounts, "data/hogmanay.rds")
ggsave("images/hogmanay.png",
width=200, height = 150,
dpi=300, units="mm", bg = "white")
# exact phrase counts
# see: https://knowyourmeme.com/memes/i-dont-know-who-needs-to-hear-this
tweetcounts <- count_all_tweets(
query = "\"I don't know who needs to hear this\"",
start_tweets = "2010-12-27T00:00:00Z",
end_tweets = "2020-01-05T00:00:00Z",
bearer_token = get_bearer(),
granularity = "day",
n = 5000
)
#reformat date
tweetcounts$time <-
parse_date_time(tweetcounts$start, orders = "ymd HMS")
#plot
tweetcounts %>% ggplot() +
geom_line(aes(time, tweet_count))
saveRDS(tweetcounts, "data/idkcounts.rds")
# animate
p <- tweetcounts %>% ggplot() +
geom_line(aes(time, tweet_count)) +
# Here comes the gganimate specific bits
transition_reveal(time) +
ease_aes('linear') +
theme_tufte(base_family = "Helvetica") +
theme(legend.position = "none") +
labs(title = "Tweet counts for 'I don't know who needs to hear this'",
x= "Day", y = "N. tweets")
anim_save("plots/idk.gif", p)
## Adding arguments
tweetcounts <- count_all_tweets(
query = "Hogmanay",
place = "Edinburgh",
is_retweet = F,
has_images = T,
start_tweets = "2019-12-27T00:00:00Z",
end_tweets = "2020-01-05T00:00:00Z",
bearer_token = get_bearer(),
granularity = "hour",
n = 500
)
## Adding arguments (users)
tweetcounts <- count_all_tweets(
query = "Hogmanay",
users = "edhogmanay",
start_tweets = "2019-12-27T00:00:00Z",
end_tweets = "2020-01-05T00:00:00Z",
bearer_token = get_bearer(),
granularity = "day",
n = 500
)
# saveRDS(tweetcounts, "data/edhogmanaycounts.rds")
## Normalizing
tweetcounts <- count_all_tweets(
query = "immigration",
start_tweets = "2015-12-27T00:00:00Z",
end_tweets = "2020-01-05T00:00:00Z",
bearer_token = get_bearer(),
granularity = "day",
n = 5000
)
# saveRDS(tweetcounts, "data/immigcounts.rds")
tweetcounts <- readRDS("data/immigcounts.rds")
baselinecounts <- count_all_tweets(
query = "therefore",
start_tweets = "2015-12-27T00:00:00Z",
end_tweets = "2020-01-05T00:00:00Z",
bearer_token = get_bearer(),
granularity = "day",
n = 5000
)
# saveRDS(baselinecounts, "data/baselinecounts.rds")
baselinecounts <- readRDS("data/baselinecounts.rds")
normalize_counts <- function(tweetcounts, baselinecounts) {
tweetcounts <- tweetcounts$tweet_count
baselinecounts <- baselinecounts$tweet_count
normalized_counts <- tweetcounts/baselinecounts
return(normalized_counts)
}
tweetcounts$normalized_count <-
normalize_counts(tweetcounts = tweetcounts,
baselinecounts = baselinecounts)
tweetcounts$day <-
parse_date_time(tweetcounts$start,
orders = "ymd HMS")
tweetcounts %>% ggplot() +
geom_line(aes(day, tweet_count)) +
theme_tufte(base_family = "Helvetica") +
theme(legend.position = "none") +
labs(title = "Immigration tweet counts",
x= "Day", y = "N. tweets")
tweetcounts %>% ggplot() +
geom_line(aes(day, normalized_count)) +
theme_tufte(base_family = "Helvetica") +
theme(legend.position = "none") +
labs(title = "Immigration tweet counts (normalized)",
x= "Day", y = "N. tweets")