Skip to content

Instantly share code, notes, and snippets.

@jonspring
Last active May 7, 2020 18:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonspring/f2ab187c90c2177bbc770606d306b10c to your computer and use it in GitHub Desktop.
Save jonspring/f2ab187c90c2177bbc770606d306b10c to your computer and use it in GitHub Desktop.
library(rtweet)
library(tidyverse)
library(tidytext)
library(ggforce)
sw <- search_tweets("Rogue, IV", n = 90000, include_rts = FALSE)
sw_clean <- sw %>%
unnest_tokens(word, text, drop = F, to_lower = T) %>%
filter(word %in% c("i", "ii", "iii", "iv", "v", "vi",
"vii", "viii", "rogue", "solo")) %>%
group_by(screen_name, created_at) %>%
mutate(rank = row_number()) %>%
ungroup() %>%
filter(rank <= 10)
sw_ranking <- sw_clean %>%
group_by(word) %>%
summarize(avg_rank = mean(rank, na.rm = T),
sd_rank = sd(rank, na.rm = T))
### Chart 1 -- Clustered points for ranking
sw_clean %>%
left_join(sw_ranking) %>%
ggplot(aes(rank,
word %>% toupper %>% fct_reorder(avg_rank) %>% fct_rev,
color = word)) +
geom_point(size = 0.2, alpha = 0.5,
position = position_jitternormal(sd_x = 0.15, sd_y = .2)) +
scale_y_discrete(name = "") +
scale_x_continuous(breaks = 1:10, minor_breaks = NULL, position = "top", name = "",
labels = c("Favorite", "2nd", "3rd", paste0(4:9,"th"), "Worst")) +
ggthemes::scale_color_tableau() +
guides(color = F) +
hrbrthemes::theme_ipsum() +
theme(panel.grid = element_blank()) +
labs(title = "Bringing Order to the Galaxy",
subtitle = "Star Wars movies as ranked on Twitter, Oct 2019")
### Chart 2 -- Avg rank vs. SD of rank
sw_ranking %>%
ggplot(aes(avg_rank, sd_rank, label = word)) +
geom_text(color = "black", size = 5) +
geom_mark_ellipse(aes(filter = avg_rank < 5 & sd_rank < 2,
label = "The Force is Strong\nwith These Ones",
description = "Most people like these"), color = "gray80") +
geom_mark_ellipse(aes(filter = sd_rank > 2 & avg_rank < 7,
label = "I Sense a\nDisturbance\nin the Force",
description = "More disagreement\nabout rankings for these"),
color = "gray80") +
geom_mark_hull(color = "gray80", radius = 0.03,
aes(filter = avg_rank > 7,
label = "Hate leads to\nsuffering")) +
geom_mark_ellipse(color = "gray80", aes(filter = word == "vii",
label = "Meh")) +
scale_x_continuous(breaks = 1:10, minor_breaks = NULL, position = "top", name = "Avg Ranking",
labels = c("Favorite", "2nd", "3rd", paste0(4:9,"th"), "Worst")) +
scale_y_reverse(breaks = c(1.2, 3), labels = c("Consensus", "Disagree"),
name = "Degree of Agreement") +
coord_cartesian(clip = "off", xlim = c(1:10), ylim = c(1.2, 3)) +
theme_minimal(base_size = 20) +
theme(panel.grid = element_blank(), axis.text.y = element_text(angle = 90, hjust = c(1,0)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment