-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.Rmd
166 lines (141 loc) · 4.59 KB
/
main.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
---
title: "Top Tweets from Chilean Presidential Candidates"
output: html_notebook
---
TODO:
- Figure out how to store secrets outside of the code ✅
- Copy code from last script ✅
- Convert output to plotly ✅
- Add `xfun::cache_rds` to cache tweets and not waste API calls ✅
- Make tweets available as fly-outs ✅
- Try to load the tweet in a box (crosstalk)
- Sentiment analysis of the replies (crosstalk)
```{r}
library(rtweet)
library(tidyverse)
library(keyring)
library(plotly)
# twitter_token <- create_token(
# app = "pacto-social-1",
# consumer_key =
# keyring::key_get("pacto-social-1-consumer_key"),
# consumer_secret =
# keyring::key_get("pacto-social-1-consumer_secret"),
# access_token =
# keyring::key_get("pacto-social-1-access_token"),
# access_secret =
# keyring::key_get("pacto-social-1-access_secret"),
# set_renv = TRUE)
```
```{r}
# Retrieving tweets from Gabriel Boric
tweets_by_boric <-
xfun::cache_rds({
get_timeline("@gabrielboric", n = 3200)
},
file = "tweets_by_boric.rds")
# Retrieving tweets from Jose Antonio Kast
tweets_by_kast <- xfun::cache_rds({
get_timeline("@joseantoniokast", n = 3200)
},
file = "tweets_by_kast.rds")
```
```{r}
# Function to filter organic tweets and rank them by engagement
get_organic_ranked <- function(df) {
df %>%
filter(is_retweet == FALSE,
is.na(reply_to_status_id),
# only tweets previous to the presidential election
created_at <= lubridate::ymd(20211220)) %>%
mutate(rank_engagement = dense_rank(desc(favorite_count + retweet_count))) %>%
arrange(rank_engagement)
}
```
Then I apply the function to both dataframes and merge the results with map_df. Next, I filter the result to keep only the 10 more popular tweets of each candidate, and select the relevant columns for the visualization.
```{r}
tweets_for_plot <-
list(tweets_by_boric,
tweets_by_kast) %>%
map_df(get_organic_ranked) %>%
filter(rank_engagement <= 10) %>%
transmute(
rank_engagement,
screen_name,
status_id,
created_at,
text,
favorite_count,
retweet_count,
engagement_count = favorite_count + retweet_count,
status_url
)
tweets_for_plot
```
```{r}
tweets_for_plot2 <- tweets_for_plot %>%
mutate(engagement_count = ifelse(screen_name == "gabrielboric",
- engagement_count,
engagement_count))
breaks_values_eng <- pretty(tweets_for_plot2$engagement_count)
abs_k <- abs(breaks_values_eng)/1000
labels_k <- ifelse(abs_k == 0, "0", str_c(abs_k, "K"))
```
```{r}
fig <- plot_ly(
data = tweets_for_plot2,
x = ~engagement_count,
y = ~rank_engagement,
text = ~abs(engagement_count),
type = 'bar',
color = ~screen_name,
colors = c("red", "blue"),
orientation = 'h',
hovertemplate = paste0(
"<b>%{text:,.0f}</b> likes and RTs | ",
format(tweets_for_plot2$created_at, "%b %d"),
" | @",
tweets_for_plot2$screen_name,
"<br><br>",
str_wrap(tweets_for_plot2$text),
"<extra></extra>"
)
) %>%
layout(barmode = 'overlay',
title = "Most popular tweets by Chilean presidential candidates",
yaxis = list(title = "Ranking",
autorange="reversed",
showgrid=T,
autotick = F, tickmode = "array", tickvals = 1:10),
xaxis = list(title = "Engagement (RTs + Likes)",
tickmode = 'array',
tickvals = breaks_values_eng,
ticktext = labels_k),
legend = list(title=list(text='<b>Candidate</b>'),
orientation = "h", # show entries horizontally
xanchor = "center", # use center of legend as anchor
x = 0.5,
y=-0.2),
hovermode = "closest",
hoverlabel = list(bgcolor = "#DFDFDF", bordercolor = "black")
)
# TODO: make them be in the same vertical position. ✅
# TODO: Fix the "Rank engagement" labels ✅
# TODO: Rename axis ✅
# TODO: Fix the horizontal axis ("Engagement") ✅
# TODO: Clean flyouts (change sign) ✅
# TODO: add twitter text as flyouts ✅
# TODO: change colours ✅
fig
```
## Retrieving replies to the tweets
## Next
NEXT: crosstalk.
https://rstudio.github.io/crosstalk/index.html
Tasks:
[✅] Filter tweets after the presidential election
[ ] Retrieve the replies of these tweets
Possible approach:
- Get all the followers of each one
- Loop through them and get their timelines
- Filter by "in reply to" using the IDs of the tweets I want