Skip to content

Instantly share code, notes, and snippets.

@csgillespie
Last active Apr 9, 2019
Embed
What would you like to do?
SatRdays Twitter wars
library("rtweet")
library("tidyverse") # Don't judge me!
# Why is this not in tidyverse
# Can we start a petition?
library("lubridate")
# Notice the superior use of "=" over "<-"
get_tweets = function(q, n = 1000, include_rts = TRUE) {
search_tweets(q = q, n = n,
include_rts = include_rts, verbose = FALSE) %>%
filter(ymd_hms(created_at) > ymd_hms("2019-04-06 00:0:0 UTC"))
}
make_plot = function(tweets, ylab) {
ggplot(tweets) +
geom_col(aes(location, tweets)) +
theme_minimal() +
coord_flip() +
xlab(NULL) + ylab(ylab)
}
ncl = get_tweets("#satrdayncl", include_rts = FALSE) %>%
bind_rows(get_tweets("#satrdaynewcastle", include_rts = FALSE)) %>%
distinct(text, .keep_all = TRUE)
la = get_tweets("#satrdayla", include_rts = FALSE)
jo = get_tweets("#satRdayJoburg", include_rts = FALSE)
tweets = tibble(location = c("Newcastle", "LA", "SA"),
tweets = c(nrow(ncl), nrow(la), nrow(jo))) %>%
mutate(location = fct_reorder(location, .x = tweets))
make_plot(tweets, "#tweets")
## ------
fav = tibble(location = c("Newcastle", "LA", "SA"),
tweets = c(sum(ncl$favorite_count),
sum(la$favorite_count),
sum(jo$favorite_count))) %>%
mutate(location = fct_reorder(location, .x = tweets))
make_plot(fav, "#fav")
## -------
users = tibble(location = c("Newcastle", "LA", "SA"),
tweets = c(length(unique(ncl$user_id)),
length(unique(la$user_id)),
length(unique(jo$user_id)))) %>%
mutate(location = fct_reorder(location, .x = tweets))
make_plot(users, "#users")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment