This week’s #TidyTuesday dataset is about Water Access Points sourced from Water point data exchange. TidyTuesday is a weekly data project aimed at the R ecosystem. The data was curtailed to select nations for this week. I only focused on Nigeria.
The Outcome
After 1980s, boreholes increased drastically in Nigeria and spread across the country
Tips i.e. challenges I faced doing it
Found out map_data function included in {ggplot2} contains more simpler maps than {sf} combined with {rnaturalearth}
transition_states normally cut-off some data points on default animation options. You can get your desired results through animate function by tweaking frames, duration etc.
Full Code
### Load libraries
# Data wrangling
library(tidyverse)
# Get TidyTuesday data
library(tidytuesdayR)
# Animation
library(gganimate)
### Get Data
tt_data<-tt_load("2021-05-04")
water<-tt_data$water
water<-water%>%
mutate(
iso3=countrycode::countryname(country_name,destination = "iso3c"),
continent=countrycode::countryname(country_name,destination = "continent")
)
# Filter Nigeria
water_nigeria<-water%>%
filter(country_name=='Nigeria',!is.na(install_year),install_year<=2021)
### Plotting
# Plot minimum viable map (MVM!)
p<-map_data("world")%>%
filter(region=='Nigeria')%>%
ggplot()+
geom_polygon(aes(x = long, y = lat, group = group), fill="grey", col = "#397D5F", size = 1)+
geom_point(data=water_nigeria,aes(x=lon_deg,y=lat_deg,col=water_source,group=install_year),size=1)+
geom_text(data=water_nigeria,aes(x=10,y=5,label= paste0(floor(install_year))),
size=15,color='#74020B')+
xlim(2.5,12)+
ylim(4,14)+
labs(
title = "Water sources in Nigeria over the years",
caption = "Plot by Saif Kabir Asif | saifkabirasif.com ",
col="Water Source"
)+
theme_minimal()+
theme(
plot.title=element_text(hjust=0.5,size=18,
face='bold'),
plot.caption=element_text(color='gray',face='italic',size=8),
plot.background = element_rect(fill='slategray2'),
panel.background = element_rect(fill='slategray2'),
legend.position = "top",
legend.background = element_rect(fill='white'),
axis.title = element_blank(),
axis.text = element_blank(),
panel.grid=element_blank()
)+
guides(color=guide_legend(override.aes = list(size=5)))+
transition_states(install_year,state_length = 1)+
shadow_mark(past=TRUE,exclude_layer = 3)+
enter_fade()
# Granular animation controls
animate(p,duration=20 ,
start_pause=0,
end_pause = 30,
detail=3,
bg='slategray2',
type='cairo',
renderer = gifski_renderer(),
width=800,
height=450
)
### Save animation
anim_save(filename = paste0("Water_Source_",Sys.Date(),".gif"),animation = last_animation())
Sharing is caring. Share this story in...
Share: Twitter Facebook LinkedIn Pocket Flipboard