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, by continent. It uses data from http://www.providingforpeacekeeping.org/contributions/
# install the following packages
library(scales)
library(ggplot2)
library(reshape)
library(Cairo)
library(RColorBrewer)
################
# Prepare data #
################
pko <- read.table("./data/Data.Full.csv", header=TRUE, sep=",")
pko$date <- as.POSIXct(pko$date)
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
aggWorld <- aggregate(total ~ date,
data = pko[pko$mission.continent=="Africa", ],
FUN = sum)
aggContinents <- aggregate(total ~ date + tcc.continent,
data = pko[pko$mission.continent=="Africa", ],
FUN = sum)
aggContinents$date <- as.POSIXct(aggContinents$date)
########
# Plot #
########
# tiny function for correct large number notation in German
point <- function(x, ...) {
format(x, ..., big.mark = ".", scientific = FALSE, trim = TRUE)
}
plot <- ggplot(aggContinents, aes(x=date, y=total, group=tcc.continent, fill=tcc.continent))
plot <- plot + geom_area()
plot <- plot + scale_fill_brewer(name="Kontinente",
palette="Spectral")
plot <- plot + labs(title="Truppenbeiträge zu VN Friedensoperationen in Afrika nach Kontinenten\n 1991 - 2012 \n", x="", y="Truppenstärke")
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))
plot <- plot + scale_y_continuous(labels = point)
plot <- plot + scale_x_datetime(limits=c(min(aggContinents$date), max(aggContinents$date)),
breaks=date_breaks("years"),
expand=c(.01,.01),
labels = date_format("%Y"))
CairoPNG(width=1280, height=800, file="PKOTCCToAfricaByContinent.png")
print(plot)
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment