One of my New Year’s resolutions was to become less of a lurker and more of a doer within the Tidy Tuesday community. There had been one dataset that I was super interested in exploring, based on Democracy and Dictatorship. Loaded the dataset and a few packages.
Code in R
library(tidytuesdayR)library(tidyverse)library(countrycode) #this package looks up ISO codes, which can be usefullibrary(ggflags) #pulls flagslibrary(gganimate) #animationsdat <- tidytuesdayR::tt_load(2024, week =45)democracy <- dat$democracy_datahead(democracy)
This dataset shows, for every year from 1950 to 2000, classifications for countries around the world. In the spirit of January 6th, I chose to focus on whether countries have free and fair elections, here shown as has_free_and_fair_election.
In the interest of this exploration, I’m not interested in colonies, which are shown as NA in the regime_category_index.
Code in R
democracy <- democracy |>## removing colonies as much as possible to get down to countriesfilter(!is.na(regime_category_index),## British Virgin Islands oddly keeps showing up in spite of status as colony country_code!="VGB")
First exploration - understanding areas with democratic changes
My first inclination in the dataset was to take out the countries that only had free and fair elections or a lack thereof, in order to understand places that had democratic progress and democratic backsliding. This required setting up a filter for this work. tabyl from the janitor package is well set up for this, as it will quickly come up with cross counts.
This gives a nice filter for this work. In order to use the ggflags package, you need for the countries to be spelled out in iso2c format. Fortunately, the amazing countrycodes package will do that for you, and in addition, it can provide the continent for you as well, for additional explorations.
Now we are ready to plot this with the help of the ggflags package. Let’s look at Europe and Africa, two continents associated with democratic progress and backsliding.
There is one big issue - we would like for this to show up as continuous bars, which your typical geoms will not do. However, we can think of the bars as parts of a rectangle, that extend from the beginning to end of each year. If we factor the countries, we can then plot them and rename the y axis at the end.
Code in R
## Let's create the y-labels for later ony_lab <- democraticchanges |>distinct(country_name,iso2,continent) |>mutate(y_mid =as.numeric(country_name),name=country_name)## For AfricaAfrica <- democraticchanges |>filter(continent=="Africa") |>ggplot(aes(xmin=year, #left boundary of the rectanglexmax=year+1, #right boundary of the rectangleymin=as.numeric(country_name)-.3, #lower boundary of the rectangleymax=as.numeric(country_name)+.3, #upper boundary of the rectanglefill=has_free_and_fair_election)) +geom_rect() +# now let's plot flags between the rectangles and the y axis ggflags::geom_flag(data=y_lab |>filter(continent=="Africa"),mapping=aes(y=y_mid,country=tolower(iso2), #ggflags needs lowercase iso2 to workx=1945),inherit.aes=FALSE) +#we want this to be individual to this layer#now to rename the y axisscale_y_continuous(breaks=y_lab$y_mid,labels=y_lab$country_name) +theme_minimal() +theme(legend.position="bottom",legend.text=element_text(size=7),legend.title=element_text(size=7)) +labs(x="",y="",fill="Has Free and Fair Elections") +scale_fill_brewer(palette="Set1") +facet_wrap(~continent,ncol=1,scales="free",strip.position="right")Europe <- democraticchanges |>filter(continent=="Europe") |>ggplot(aes(xmin=year,xmax=year+1,ymin=as.numeric(country_name)-.3,ymax=as.numeric(country_name)+.3,fill=has_free_and_fair_election)) +geom_rect() + ggflags::geom_flag(data=y_lab |>filter(continent=="Europe"),mapping=aes(y=y_mid,country=tolower(iso2),x=1945),inherit.aes=FALSE) +scale_y_continuous(breaks=y_lab$y_mid,labels=y_lab$country_name) +theme_minimal() +theme(legend.position="bottom",legend.text=element_text(size=7),legend.title=element_text(size=7)) +labs(x="",y="",fill="Has Free and Fair Elections") +scale_fill_brewer(palette="Set1") +facet_wrap(~continent,ncol=1,scales="free",strip.position="right")## Let's take advantage of patchwork to plot these graphs next to each other with a common legendlibrary(patchwork)Europe + Africa +plot_layout(guides ='collect') &theme(legend.position ='bottom')
This is fun! Let’s make a map and animate it.
gganimation for a map to show progress/backsliding
One can see at a glance that, in Europe, there appears to be a democratic progression. Similarly, in Africa, there is a progression, but there is also backsliding. It would also be really useful to see this as a map - moreover, it would be really interesting to see as an animated map.
rnaturalearth is a package that allows you to easily download map borders. Patrice Ferlet keeps a dataset of the centroid for countries that we can plot the flags on as well.
Then we can layer this map easily. Notice that the latitude and longitude signs are not typical of a map - the excellent metR package includes helpful themes and scales for meteorological data, which applies to maps as well!
This map is a bit overwhelming, let’s use gganimate and focus on Europe and Africa. Comments below to walk through the European one. Because it’s tough to render, I saved it locally and then loaded it up on the website. Hooray!
Code in R
#Europe#First a base layerEurope2 <- map |>filter(continent=="Europe") |>ggplot() +# note that fill is the inside and color is the border in geom_sfgeom_sf(mapping=aes(geometry=geometry),fill="white",color="black") +#Then the democracy datageom_sf(data=democracymap2 |>filter(continent=="Europe"),mapping=aes(geometry=geometry),color="black",show.legend=FALSE) + ggflags::geom_flag(data=democracymap2 |>filter(continent=="Europe", has_free_and_fair_election==TRUE),mapping=aes(y=lat,x=lon,country=tolower(iso2)),inherit.aes=FALSE) +# gganimate allows you to use {closest_state} to fill in the transition labellabs(title='Year: {closest_state}') +#metR scalesscale_x_longitude() +scale_y_latitude() +#gganimate transitions for the giftransition_states(year) +theme_bw() +coord_sf(xlim=c(-20,40),ylim=c(30,70),default_crs=sf::st_crs(4326))#anim_save(filename="img/Europe.gif",animation=Europe2)