Instantly share code, notes, and snippets.

Embed
What would you like to do?
This piece of code produces a graphic that depicts trends in contributions to UN peacekeeping on the African continent, both by all countries worldwide and by African countries only. It uses data from http://www.providingforpeacekeeping.org/contributions/
# install the following packages
library(scales)
library(ggplot2)
library(reshape)
library(Cairo)
################
# Prepare data #
################
pko <- read.table("./data/Data.Full.csv", header=TRUE, sep=",")
pko$date <- as.POSIXct(pko$date)
# subset specific time period; comment this out if you want full coverage
pko <- pko[pko$date < as.POSIXct("2013-01-01"), ]
pko <- pko[pko$date > as.POSIXct("1990-12-31"), ]
# aggregate data for African contributions to PKOs per month
aggWorld <- aggregate(total ~ date,
data = pko[pko$mission.continent=="Africa", ],
FUN = sum)
aggAfrica <- aggregate(total ~ date,
data = pko[pko$tcc.continent=="Africa" & pko$mission.continent=="Africa", ],
FUN = sum)
# combine world and Africa data in one data frame & relabel
TCCtoAfrica <- merge(aggWorld, aggAfrica, by="date")
names(TCCtoAfrica) <- c("date", "contWorldwide", "contbyAfrica")
########
# Plot #
########
# tiny function to correct large number notation in German
point <- function(x, ...) {
format(x, ..., big.mark = ".", scientific = FALSE, trim = TRUE)
}
# melt data for convenient ggplot plotting
pm <- melt(TCCtoAfrica,
id.vars=("date"),
measure.vars=c("contWorldwide", "contbyAfrica"))
plot <- ggplot(pm, aes(x=date, y=value, group=variable, colour=variable))
plot <- plot + geom_line()
plot <- plot + labs(title="Truppenbeiträge zu VN Friedensoperationen in Afrika, 1991 - 2012 \n", x="Daten: International Peace Institute / Perry and Smith 2013; Eigene Darstellung", y="Truppenstärke")
plot <- plot + labs(y="Truppenstärke", x="")
plot <- plot + theme_bw(base_size=22)
plot <- plot + theme(axis.text.x=element_text(hjust=1.1, angle=45),
axis.title.y=element_text(vjust=0.3),
axis.title.x=element_text(size=11, lineheight=15, vjust=-.7, hjust=0),
legend.key=element_blank())
plot <- plot + scale_y_continuous(labels = point)
plot <- plot + scale_x_datetime(limits=c(min(pm$date), max(pm$date)),
breaks=date_breaks("years"),
expand=c(.01,.01),
labels = date_format("%Y"))
plot <- plot + scale_colour_manual("", values = c("grey", "black"), labels = c(" Weltweite Truppenbeiträge \n zu VN Friedensoperationen \n in Afrika\n", " Afrikanische Beiträge zu \n VN Friedensoperationen \n in Afrika"))
# adjust width and height for different resolutions or use CairoPDF
CairoPNG(width=1280, height=800, file="PKOContribToAfrica.png")
print(plot)
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment