Lib
#Open hashtag if you dont have the lib.
#install.packages("gganimate")
#install.packages("tidyverse")
#install.packages("janitor")
#install.packages("scales")
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.5.3
## -- Attaching packages ------------------------------------------------------------------------------------------------------------- tidyverse 1.2.1 --
## v ggplot2 3.1.0 v purrr 0.3.0
## v tibble 2.0.1 v dplyr 0.7.8
## v tidyr 0.8.3 v stringr 1.4.0
## v readr 1.3.1 v forcats 0.4.0
## Warning: package 'tidyr' was built under R version 3.5.3
## Warning: package 'readr' was built under R version 3.5.3
## Warning: package 'stringr' was built under R version 3.5.3
## Warning: package 'forcats' was built under R version 3.5.3
## -- Conflicts ---------------------------------------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(janitor)
## Warning: package 'janitor' was built under R version 3.5.3
library(stringr)
Data
gdp <- read.csv("./animated_bar_charts_in_R-master/animated_bar_charts_in_R-master/data/GDP_Data.csv")
Dataset Overview
#summary(gdp)
str(gdp)
## 'data.frame': 269 obs. of 16 variables:
## $ ï..Series.Name: Factor w/ 4 levels "","Data from database: World Development Indicators",..: 3 3 3 3 3 3 3 3 3 3 ...
## $ Series.Code : Factor w/ 2 levels "","NY.GDP.MKTP.CD": 2 2 2 2 2 2 2 2 2 2 ...
## $ Country.Name : Factor w/ 265 levels "","Afghanistan",..: 2 3 4 5 6 7 8 10 11 12 ...
## $ Country.Code : Factor w/ 265 levels "","ABW","AFG",..: 3 5 60 11 6 4 12 9 10 2 ...
## $ X1990..YR1990.: Factor w/ 222 levels "","..","1009455483.87097",..: 2 67 188 2 7 15 159 38 74 202 ...
## $ X2000..YR2000.: Factor w/ 245 levels "","..","10018778713285.6",..: 2 128 172 2 42 233 217 109 73 70 ...
## $ X2009..YR2009.: Factor w/ 249 levels "","..","10181021770.4326",..: 29 22 43 217 143 219 28 133 233 104 ...
## $ X2010..YR2010.: Factor w/ 249 levels "","..","10003670690.3497",..: 53 20 55 195 135 234 15 157 244 105 ...
## $ X2011..YR2011.: Factor w/ 250 levels "","..","100351700000",..: 58 25 77 192 140 14 15 187 6 109 ...
## $ X2012..YR2012.: Factor w/ 249 levels "","..","10044524034458.6",..: 76 23 80 205 132 28 20 191 10 105 ...
## $ X2013..YR2013.: Factor w/ 250 levels "","..","10146688738.6529",..: 82 26 84 210 137 36 18 192 13 107 ...
## $ X2014..YR2014.: Factor w/ 249 levels "","..","100948236941.182",..: 85 31 90 210 137 44 26 187 17 111 ...
## $ X2015..YR2015.: Factor w/ 248 levels "","..","1003001432752.72",..: 85 17 61 211 118 20 39 200 11 111 ...
## $ X2016..YR2016.: Factor w/ 246 levels "","..","10001193315.1966",..: 78 27 60 207 121 5 52 196 11 113 ...
## $ X2017..YR2017.: Factor w/ 240 levels "","..","1015539017536.5",..: 76 32 62 204 122 23 51 205 17 114 ...
## $ X2018..YR2018.: Factor w/ 2 levels "","..": 2 2 2 2 2 2 2 2 2 2 ...
head(gdp)
## ï..Series.Name Series.Code Country.Name Country.Code
## 1 GDP (current US$) NY.GDP.MKTP.CD Afghanistan AFG
## 2 GDP (current US$) NY.GDP.MKTP.CD Albania ALB
## 3 GDP (current US$) NY.GDP.MKTP.CD Algeria DZA
## 4 GDP (current US$) NY.GDP.MKTP.CD American Samoa ASM
## 5 GDP (current US$) NY.GDP.MKTP.CD Andorra AND
## 6 GDP (current US$) NY.GDP.MKTP.CD Angola AGO
## X1990..YR1990. X2000..YR2000. X2009..YR2009. X2010..YR2010.
## 1 .. .. 12439087076.7667 15856574731.4411
## 2 2028553750 3480355258.04122 12044208085.864 11926957254.6288
## 3 62045099642.7774 54790245600.5846 137211039898.193 161207268655.392
## 4 .. .. 678000000 576000000
## 5 1029048481.88051 1434429703.33518 3660530702.97305 3355695364.23841
## 6 11228764963.1618 9129594818.60749 70307163678.1895 83799496611.6049
## X2011..YR2011. X2012..YR2012. X2013..YR2013. X2014..YR2014.
## 1 17804292964.1045 19907317065.6667 20561069558.2152 20484885119.7348
## 2 12890866742.6533 12319784886.2038 12776280961.155 13228247844.1247
## 3 200019057307.655 209058991952.125 209755003250.664 213810022462.428
## 4 574000000 644000000 641000000 643000000
## 5 3442062830.13622 3164615186.94591 3281585236.32501 3350736367.25488
## 6 111789686464.26 128052853643.447 136709862831.308 145712200312.505
## X2015..YR2015. X2016..YR2016. X2017..YR2017. X2018..YR2018.
## 1 19907111418.9938 19046357714.4928 19543976895.4248 ..
## 2 11386931489.7968 11883682170.8236 13038538300.2644 ..
## 3 165979277276.907 160129866569.935 167555280113.181 ..
## 4 661000000 653000000 634000000 ..
## 5 2811489408.89431 2877311946.90265 3012914131.16971 ..
## 6 116193649124.475 101123851090.473 122123822333.73 ..
Data Prepro
#select required columns
gdp <- gdp %>% select(3:15)
#filter only country rows
gdp <- gdp[1:217,]
gdp_tidy <- gdp %>%
mutate_at(vars(contains("YR")),as.numeric) %>%
gather(year,value,3:13) %>%
janitor::clean_names() %>% #fix column name
mutate(year = as.numeric(stringr::str_sub(year,1,4)))
## Warning in evalq(as.numeric(stringr::str_sub(year, 1, 4)), <environment>):
## NAs introduced by coercion
# NOT WORK (???)
See GDP Tidy
#year & value column still have a problem, so we are using data from website.
summary(gdp_tidy)
## country_name country_code year value
## Afghanistan : 11 ABW : 11 Min. : NA Min. : 2.0
## Albania : 11 AFG : 11 1st Qu.: NA 1st Qu.: 47.0
## Algeria : 11 AGO : 11 Median : NA Median :114.0
## American Samoa: 11 ALB : 11 Mean :NaN Mean :114.4
## Andorra : 11 AND : 11 3rd Qu.: NA 3rd Qu.:179.0
## Angola : 11 ARE : 11 Max. : NA Max. :250.0
## (Other) :2321 (Other):2321 NA's :2387
str(gdp_tidy)
## 'data.frame': 2387 obs. of 4 variables:
## $ country_name: Factor w/ 265 levels "","Afghanistan",..: 2 3 4 5 6 7 8 10 11 12 ...
## $ country_code: Factor w/ 265 levels "","ABW","AFG",..: 3 5 60 11 6 4 12 9 10 2 ...
## $ year : num NA NA NA NA NA NA NA NA NA NA ...
## $ value : num 2 67 188 2 7 15 159 38 74 202 ...
head(gdp_tidy)
## country_name country_code year value
## 1 Afghanistan AFG NA 2
## 2 Albania ALB NA 67
## 3 Algeria DZA NA 188
## 4 American Samoa ASM NA 2
## 5 Andorra AND NA 7
## 6 Angola AGO NA 15
Animated Bar
Lib
library(tidyverse)
library(gganimate)
## Warning: package 'gganimate' was built under R version 3.5.3
library(png)
Data
gdp_tidy <- read_csv("./animated_bar_charts_in_R-master/animated_bar_charts_in_R-master/data/gdp_tidy.csv")
## Parsed with column specification:
## cols(
## country_name = col_character(),
## country_code = col_character(),
## year = col_double(),
## value = col_double()
## )
summary(gdp_tidy)
## country_name country_code year value
## Length:2387 Length:2387 Min. :1990 Min. :8.824e+06
## Class :character Class :character 1st Qu.:2009 1st Qu.:4.435e+09
## Mode :character Mode :character Median :2012 Median :2.020e+10
## Mean :2010 Mean :3.241e+11
## 3rd Qu.:2015 3rd Qu.:1.384e+11
## Max. :2017 Max. :1.939e+13
## NA's :193
str(gdp_tidy)
## Classes 'spec_tbl_df', 'tbl_df', 'tbl' and 'data.frame': 2387 obs. of 4 variables:
## $ country_name: chr "Afghanistan" "Albania" "Algeria" "American Samoa" ...
## $ country_code: chr "AFG" "ALB" "DZA" "ASM" ...
## $ year : num 1990 1990 1990 1990 1990 1990 1990 1990 1990 1990 ...
## $ value : num NA 2.03e+09 6.20e+10 NA 1.03e+09 ...
## - attr(*, "spec")=
## .. cols(
## .. country_name = col_character(),
## .. country_code = col_character(),
## .. year = col_double(),
## .. value = col_double()
## .. )
head(gdp_tidy)
## # A tibble: 6 x 4
## country_name country_code year value
## <chr> <chr> <dbl> <dbl>
## 1 Afghanistan AFG 1990 NA
## 2 Albania ALB 1990 2028553750
## 3 Algeria DZA 1990 62045099643.
## 4 American Samoa ASM 1990 NA
## 5 Andorra AND 1990 1029048482.
## 6 Angola AGO 1990 11228764963.
Data Manipulation
gdp_formatted <- gdp_tidy %>%
group_by(year) %>%
# The * 1 makes it possible to have non-integer ranks while sliding
mutate(rank = rank(-value),
Value_rel = value/value[rank==1],
Value_lbl = paste0(" ",round(value/1e9))) %>%
group_by(country_name) %>%
filter(rank <=10) %>%
ungroup()
Building Static Plots
staticplot = ggplot(gdp_formatted, aes(rank, group = country_name,
fill = as.factor(country_name), color = as.factor(country_name))) +
geom_tile(aes(y = value/2,
height = value,
width = 0.9), alpha = 0.8, color = NA) +
geom_text(aes(y = 0, label = paste(country_name, " ")), vjust = 0.2, hjust = 1) +
geom_text(aes(y=value,label = Value_lbl, hjust=0)) +
coord_flip(clip = "off", expand = FALSE) +
scale_y_continuous(labels = scales::comma) +
scale_x_reverse() +
guides(color = FALSE, fill = FALSE) +
theme(axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
legend.position="none",
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.grid.major.x = element_line( size=.1, color="grey" ),
panel.grid.minor.x = element_line( size=.1, color="grey" ),
plot.title=element_text(size=25, hjust=0.5, face="bold", colour="grey", vjust=-1),
plot.subtitle=element_text(size=18, hjust=0.5, face="italic", color="grey"),
plot.caption =element_text(size=8, hjust=0.5, face="italic", color="grey"),
plot.background=element_blank(),
plot.margin = margin(2,2, 2, 4, "cm"))
Animation
anim = staticplot + transition_states(year, transition_length = 4, state_length = 1) +
view_follow(fixed_x = TRUE) +
labs(title = 'GDP per Year : {closest_state}',
subtitle = "Top 10 Countries",
caption = "GDP in Billions USD | Data Source: World Bank Data")
Rendering
# For GIF
animate(anim, 200, fps = 20, width = 1200, height = 1000,
renderer = gifski_renderer("gganim.gif"))

# For MP4
#animate(anim, 200, fps = 20, width = 1200, height = 1000,
# renderer = ffmpeg_renderer()) -> for_mp4
#anim_save("animation.mp4", animation = for_mp4 )
#REF
#https://towardsdatascience.com/create-animated-bar-charts-using-r-31d09e5841da