Last active
March 24, 2019 08:44
-
-
Save giocomai/edc556465da299fc43c084b1cdef2557 to your computer and use it in GitHub Desktop.
Nations in transit animated line chart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (!require("pacman")) install.packages("pacman") | |
pacman::p_load("tidyverse") | |
pacman::p_load("gganimate") | |
pacman::p_load("gifski") | |
nit <- tibble::tribble( | |
~Country, ~`2001`, ~`2002`, ~`2003`, ~`2004`, ~`2005`, ~`2006`, ~`2007`, ~`2008`, ~`2009`, ~`2010`, ~`2011`, ~`2012`, ~`2013`, ~`2014`, ~`2015`, ~`2016`, ~`2017`, ~`2018`, | |
"Albania", 4.42, 4.25, 4.17, 4.13, 4.04, 3.79, 3.82, 3.82, 3.82, 3.93, 4.04, 4.14, 4.25, 4.18, 4.14, 4.14, 4.14, 4.11, | |
"Bosnia Herzegovina", 5.17, 4.83, 4.54, 4.29, 4.18, 4.07, 4.04, 4.11, 4.18, 4.25, 4.32, 4.36, 4.39, 4.43, 4.46, 4.5, 4.54, 4.64, | |
"Bulgaria", 3.42, 3.33, 3.38, 3.25, 3.18, 2.93, 2.89, 2.86, 3.04, 3.04, 3.07, 3.14, 3.18, 3.25, 3.29, 3.25, 3.36, 3.39, | |
"Croatia", 3.54, 3.54, 3.79, 3.83, 3.75, 3.71, 3.75, 3.64, 3.71, 3.71, 3.64, 3.61, 3.61, 3.68, 3.68, 3.68, 3.71, 3.75, | |
"Hungary", 2.13, 2.13, 1.96, 1.96, 1.96, 2, 2.14, 2.14, 2.29, 2.39, 2.61, 2.86, 2.89, 2.96, 3.18, 3.29, 3.54, 3.71, | |
"Kosovo", NA, NA, NA, 5.5, 5.32, 5.36, 5.36, 5.21, 5.11, 5.07, 5.18, 5.18, 5.25, 5.14, 5.14, 5.07, 4.96, 4.93, | |
"Macedonia", 4.04, 4.46, 4.29, 4, 3.89, 3.82, 3.82, 3.86, 3.86, 3.79, 3.82, 3.89, 3.93, 4, 4.07, 4.29, 4.43, 4.36, | |
"Montenegro", NA, NA, NA, 3.83, 3.79, 3.89, 3.93, 3.79, 3.79, 3.79, 3.82, 3.82, 3.82, 3.86, 3.89, 3.93, 3.89, 3.93, | |
"Romania", 3.67, 3.71, 3.63, 3.58, 3.39, 3.39, 3.29, 3.36, 3.36, 3.46, 3.43, 3.43, 3.5, 3.46, 3.46, 3.46, 3.39, 3.46, | |
"Serbia", NA, NA, NA, 3.83, 3.75, 3.71, 3.68, 3.79, 3.79, 3.71, 3.64, 3.64, 3.64, 3.64, 3.68, 3.75, 3.82, 3.96 | |
) | |
nit_long <- nit %>% | |
tidyr::gather(Year, Score, dplyr::contains("0")) %>% | |
mutate(Year = as.numeric(Year)) %>% | |
mutate(Score = as.numeric(paste0("-", Score))) | |
selectedCountries <- c("Serbia", "Hungary") | |
nit_selected <- nit_long %>% | |
filter(is.element(el = Country, set = selectedCountries)) | |
nit_selected_animated <- | |
nit_selected %>% | |
ggplot(mapping = aes(x = Year, | |
y = Score, | |
group = Country, | |
label = as.character(round(x = Score, digits = 2)), | |
fill = Country, | |
colour = Country)) + | |
geom_line(size = 1.5) + | |
geom_point(stat='identity', | |
colour = "white", | |
pch=21, | |
size=12) + | |
geom_text(color="white", size=3) + | |
geom_text(mapping = aes(label = Country), nudge_x = 1.8, colour = "darkgrey") + | |
scale_y_continuous(name = "", limits = c(-7,0), breaks = scales::pretty_breaks(7)) + | |
scale_x_continuous(name = "", breaks = 2001:2018) + | |
transition_reveal(along = Year) + | |
theme_minimal() + | |
theme(legend.position="none", | |
axis.text.x = element_text(angle = 45, hjust = 1)) | |
dir.create("gif", showWarnings = FALSE) | |
animate(plot = nit_selected_animated, | |
width = 800, | |
height = 600, | |
res = 120, | |
renderer = | |
gifski_renderer(file = | |
file.path("gif", "nit_selected_animated.gif"))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment