Skip to content

Instantly share code, notes, and snippets.

View felixhaass's full-sized avatar

Felix Haass felixhaass

View GitHub Profile
library(ggplot2)
library(Cairo)
library(scales)
library(RCurl)
library(rjson)
#################################
# Data preparation from web API #
#################################
@felixhaass
felixhaass / rankAfricanTCCs.R
Created October 19, 2013 11:04
This code ranks all African states that have contributed troops to UN Peacekeeping Operations by summing their monthly contributions. Note that this does not equal each country's actual troop deployment over time, since the scripts simply sums each monthly contribution which might not actually differ from the country's contribution in the preced…
pko <- read.table("./data/Data.Full.csv", header=TRUE, sep=",")
pkoAfricaTotal <- pko[pko$tcc.continent=="Africa", ] # subset all African TCCs
contribSum <- rowsum(pkoAfricaTotal$total,
pkoAfricaTotal$tcc) # sum all contributions
topTCC <- contribSum[order(contribSum[,1], decreasing=TRUE), ] # order countries
# convert to dataframe
@felixhaass
felixhaass / TCCs_Africa.R
Last active December 25, 2015 22:59
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.
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)
################
@felixhaass
felixhaass / PKOcontributionsToAfrica_bycontinent.R
Last active December 25, 2015 18:39
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 #
@felixhaass
felixhaass / PKOcontributionsToAfrica.R
Last active December 25, 2015 18:39
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 #
################
@felixhaass
felixhaass / Spacewalks.R
Created April 22, 2015 17:08
Plot annual number of spacewalks by Russia & US
library(readr)
# read
eva <- read_csv("Extra-vehicular_Activity__EVA__-_US_and_Russia.csv")
# extract years from somewhat inconsisten Date variable
eva$year <- stringi::stri_extract_last_regex(eva$Date, "\\d{4}") # get year
eva$year <- as.numeric(eva$year)
# prepare plot data
@felixhaass
felixhaass / BnH.R
Last active August 29, 2015 14:14
Population Development of Bosnia and Herzegovina
library(dplyr)
library(countrycode)
WDI("BA", indicator = c("SP.POP.TOTL"), start = 1975, end = 2011) %>%
select(year, SP.POP.TOTL) %>%
plot(., xlab = "", ylab = "",
main = "Population Development of Bosnia and Herzegovina")
mtext("Source: World Bank", side = 1, line = 3, adj = 1, cex = .6)
@felixhaass
felixhaass / ssmt14nationalities.R
Last active August 29, 2015 14:04
Breakdown of ECPR SSMT14 Nationalities (Edit: In fact, this rather counts the countries of participants' institution affiliation.)
library(XML)
library(ggplot2)
# This is a snapshot from the ECPR Moodle's Participant, copy & pasted in HTML
# I've deleted names for privacy reasons
ecpr <- readLines("https://www.dropbox.com/sh/9swobnm3s1lth00/AABHowrIWds1G4pfybDdU0q8a/Participants.htm?dl=1")
# convert to data.frame
ecpr_df <- as.data.frame(readHTMLTable(ecpr, header = FALSE))
@felixhaass
felixhaass / GoT_screentime.R
Last active August 29, 2015 14:00
This code reproduces the data and plots in this blog post on each Game of Thrones character's screen time on the show in seasons 1 to 3.
got <- readLines("GoT_screentime_full.txt", encoding="UTF-8")
gotdf <- data.frame(name=vector(),
minutes=vector(),
seconds=vector(),
seasons=vector(),
episode_count=vector())
for(i in 1:length(got)) {
char <- strsplit(got[i], split=" = |\\. |\\(|\\:|; |)")