Last active
December 25, 2015 22:59
-
-
Save felixhaass/7054062 to your computer and use it in GitHub Desktop.
This code maps African states' monthly contributions to UN Peacekeeping operations, using data from http://www.providingforpeacekeeping.org/contributions/. Darker colors mean more contributions. The script generates a .gif of about 5Mb size.
This file contains hidden or 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
library(rworldmap) | |
library(classInt) | |
library(Cairo) | |
library(RColorBrewer) | |
# note that the 'animation' package requires ImageMagick to convert .png | |
# to GIF: http://www.imagemagick.org/script/binary-releases.php | |
library(animation) | |
################ | |
# Prepare data # | |
################ | |
pkoTCC <- read.table("./data/Data.TCC.csv", header=TRUE, sep=",") | |
pkoTCC$date <- as.POSIXct(pkoTCC$date) | |
# subset African countries from all TCCs & restrict to 1990 to Dec 2012 | |
pkoAfricaTotal <- pkoTCC[pkoTCC$tcc.continent=="Africa" & pkoTCC$date <= as.POSIXct("2012-12-31"), ] | |
# create intervals for contributions | |
classInt <- classIntervals(pkoAfricaTotal$total.sum, | |
n=7, | |
style = "fisher") | |
catMethod = classInt[["brks"]] | |
# generate color palette | |
mypal <- brewer.pal(7, "Blues") | |
####################### | |
# Create animated GIF # | |
####################### | |
mapDevice() | |
saveGIF({ | |
# loop subsets African states' monthly troop contributions | |
# between 1/1991 and 12/2012 and plots this using rworldmap | |
for(year in 1991:2012) { | |
for(month in 1:12) { | |
# add zero to every month | |
if (month < 10) { | |
pkoAfricaSubset <- pkoAfricaTotal[grep(paste(year, "-", "0", month, sep=""), | |
pkoAfricaTotal$date), ] | |
} | |
else { | |
pkoAfricaSubset <- pkoAfricaTotal[grep(paste(year, "-", month, sep=""), | |
pkoAfricaTotal$date), ] | |
} | |
# prepare map data | |
sPDF <- joinCountryData2Map(pkoAfricaSubset, | |
joinCode="ISO3", | |
nameJoinColumn="tcc.iso3.alpha") | |
# plot map data | |
mapCountryData(sPDF, | |
nameColumnToPlot="total.sum", | |
catMethod = catMethod, | |
colourPalette = mypal, | |
xlim=c(-20, 60), ylim=c(-40, 40), | |
mapTitle = paste("Uniformed Peacekeeping Contributions by African States", | |
month, | |
"/", | |
year)) | |
# uncomment this to add country names to map | |
# country_names <- sPDF[!is.na(sPDF$total.sum), ] | |
# text(country_names$LON, country_names$LAT, country_names$ADMIN, cex=0.6) | |
ani.pause() # required by animation package | |
} | |
} | |
# additional options for saveGIF() | |
}, movie.name = "TCC.gif", | |
outdir = paste(getwd(), "/output/", sep=""), | |
interval = 0.3, | |
ani.height=600, | |
ani.width=600, | |
ani.dev="CairoPNG", | |
title = "Providing for Peacekeeping in Africa", | |
description = "Contributions by African States to UN Peacekeeping operations from 1991 to 2012. Includes both military and police contributions. Darker colors indicate greater troop contributions.", | |
clean=TRUE) | |
dev.off() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment