Skip to content

Instantly share code, notes, and snippets.

@giocomai
Last active September 12, 2018 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giocomai/f730c778785cad9293caa52a1ee13272 to your computer and use it in GitHub Desktop.
Save giocomai/f730c778785cad9293caa52a1ee13272 to your computer and use it in GitHub Desktop.
Words disappearing from SOTEU animated
# load libraries
library("gganimate")
library("tidyverse")
# introduce data
structure(list(id = c("Barroso\n2010", "Barroso\n2010", "Barroso\n2010",
"Barroso\n2011", "Barroso\n2011", "Barroso\n2011", "Barroso\n2012",
"Barroso\n2012", "Barroso\n2012", "Barroso\n2013", "Barroso\n2013",
"Barroso\n2013", "Juncker\n2015", "Juncker\n2015", "Juncker\n2015",
"Juncker\n2016", "Juncker\n2016", "Juncker\n2016", "Juncker\n2017",
"Juncker\n2017", "Juncker\n2017", "Juncker\n2018", "Juncker\n2018",
"Juncker\n2018"),
word = c("crisis", "growth", "refugee", "crisis",
"growth", "refugee", "crisis", "growth", "refugee", "crisis",
"growth", "refugee", "crisis", "growth", "refugee", "crisis",
"growth", "refugee", "crisis", "growth", "refugee", "crisis",
"growth", "refugee"),
n = c(8, 26, 0, 12, 15, 0, 18, 16, 0, 24,
8, 1, 27, 9, 34, 9, 5, 4, 2, 3, 5, 1, 0, 0)),
row.names = c(NA, -24L),
class = c("tbl_df",
"tbl", "data.frame")) %>%
# make graph
ggplot(mapping = aes(x = id, y = n, label = n)) +
# and animate
transition_states(states = word,
transition_length = 1,
state_length = 4) +
geom_col(fill = "steelblue4") +
scale_x_discrete(name = "") +
scale_y_continuous(name = "Number of mentions") +
theme_minimal(base_size = 18, base_family = "Carlito") +
theme(plot.title = element_text(hjust = 0.5), legend.position="none") +
# include in the title which element is being shown
labs(title = "Words disappearing from #SOTEU speeches:\n{closest_state}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment