forked from ulfelder/dart-throwing-chimp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trends-over-time-in-state-sponsored-mass-killing
215 lines (191 loc) · 11.3 KB
/
trends-over-time-in-state-sponsored-mass-killing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# Code to make charts in "Trends Over Time in State-Sponsored Mass Killing" (25 July 2013)
# http://dartthrowingchimp.wordpress.com/2013/07/25/trends-over-time-in-state-sponsored-mass-killing/
# DATA
# Clear workspace.
rm(list=ls(all=TRUE))
# Get .csv from this link to my Google Drive
# https://docs.google.com/file/d/0B5wyt4eDq98GQkJPcGx6cFNpd3M/edit?usp=sharing
# Load it.
df <- read.csv("masskillings.csv", header = TRUE)
# TIME-SERIES PLOTS
# Create annual summaries
onsets <- tapply(ifelse(df$start==1, 1, 0), df$year, sum, na.rm = TRUE)
ongoings <- tapply(ifelse(df$ongoing==1, 1, 0), df$year, sum, na.rm = TRUE)
years <- seq(from=1945, to=2013, by=1)
countries <- tapply(ifelse(is.na(df$age)== FALSE, 1, 0), df$year, sum, na.rm = TRUE)
datyr <- as.data.frame(cbind(years, countries, onsets, ongoings))
datyr$incidence <- 100 * (datyr$onsets / datyr$countries)
datyr$prevalence <- 100 * (datyr$ongoings / datyr$countries)
datyr <- datyr[which(datyr$year < 2013),] # Remove current (incomplete) year
jpeg(file="mass.killing.trends.1946.2012.jpg",
width=6, height=8, units="in", bg="white", res=150)
par(mfrow=c(3,1))
plot(datyr$years, datyr$onsets, type="h", col="orange", lwd=2,
ylim=c(0,10), xlab="", ylab="no. of events", main = "Counts of Onsets")
plot(datyr$years, datyr$incidence, type="l", col="red", lwd=2,
ylim=c(0,10), xlab="", ylab="% of countries", main = "Incidence")
plot(datyr$years, datyr$prevalence, type="l", col="brown", lwd=2,
ylim=c(0,25), xlab="", ylab="% of countries", main = "Prevalence")
dev.off()
# MAPS AND ANIMATION
library(rworldmap)
# NOTE: Because country names must match, ensuing code is version dependent. This was run
# using version 1.02-6. You can check which version you're running with:
# packageDescription('rworldmap')$Version
# To install the latest version, use:
# install.packages("rworldmap", repos="http://R-Forge.R-project.org")
# Rename some countries to match the names used in 'rworldmap'
df$country <- as.character(df$country)
df$country <- replace(df$country, df$country=="Ivory Coast", "Cote d'Ivoire")
df$country <- replace(df$country, df$country=="Congo-Brazzaville", "Congo")
df$country <- replace(df$country, df$country=="Congo-Kinshasa", "Democratic Republic of the Congo")
df$country <- replace(df$country, df$country=="Iran", "Iran (Islamic Republic of)")
df$country <- replace(df$country, df$country=="Macedonia", "The former Yugoslav Republic of Macedonia")
df$country <- replace(df$country, df$country=="Laos", "Lao People's Democratic Republic")
df$country <- replace(df$country, df$country=="Moldova", "Republic of Moldova")
df$country <- replace(df$country, df$country=="Vietnam", "Viet Nam")
df$country <- replace(df$country, df$country=="Syria", "Syrian Arab Republic")
df$country <- replace(df$country, df$country=="Tanzania", "United Republic of Tanzania")
df$country <- replace(df$country, df$country=="North Korea", "Korea, Democratic People's Republic of")
df$country <- replace(df$country, df$country=="South Korea", "Korea, Republic of")
df$country <- replace(df$country, df$country=="Timor Leste", "Timor-Leste")
df$country <- replace(df$country, df$country=="Myanmar", "Burma")
# Function for adding empty country-years for pre- or post-independence spells
additup <- function(x) {
id <- rep(x, each = length(seq(min(df$year), max(df$year), 1)))
yr <- rep(seq(min(df$year), max(df$year), 1), times = length(x))
d <- as.data.frame(cbind(id, yr))
names(d) <- c("country", "year")
rm(id, yr)
d$country <- as.character(d$country)
d$year <- as.numeric(as.character(d$year))
df <- merge(df, d, all = TRUE)
return(df)
}
# Paint former Soviet republics with values for "Soviet Union" pre-1991
ssrs <- c("Russia", "Estonia", "Latvia", "Lithuania", "Belarus",
"Republic of Moldova", "Georgia", "Armenia", "Azerbaijan", "Tajikistan",
"Turkmenistan", "Kazakhstan", "Uzbekistan", "Kyrgyzstan", "Ukraine")
df <- additup(ssrs)
ussr <- which(df$year < 1991 & (df$country=="Russia" | df$country=="Estonia" | df$country=="Latvia" |
df$country=="Lithuania" | df$country=="Belarus" | df$country=="Republic of Moldova" |
df$country=="Georgia" | df$country=="Armenia" | df$country=="Azerbaijan" |
df$country=="Tajikistan" | df$country=="Turkmenistan" | df$country=="Kazakhstan" |
df$country=="Uzbekistan" | df$country=="Kyrgyzstan" | df$country=="Ukraine") )
df$ongoing <- replace(df$ongoing, ussr, 0)
df$start <- replace(df$start, ussr, 0)
# Same for Czechoslovakia
czech <- c("Czech Republic", "Slovakia")
df <- additup(czech)
czechgo1 <- which( ( df$year==1946 | (df$year>1947 & df$year<1964) ) &
(df$country=="Czech Republic" | df$country=="Slovakia") )
czechgo0 <- which( ( df$year==1947 | (df$year>=1964 & df$year<1993) ) &
(df$country=="Czech Republic" | df$country=="Slovakia") )
czechst1 <- which(df$year==1948 & (df$country=="Czech Republic" | df$country=="Slovakia") )
czechst0 <- which((df$year<1948 | (df$year>1948 & df$year<1993) ) &
(df$country=="Czech Republic" | df$country=="Slovakia") )
df$ongoing <- replace(df$ongoing, czechgo1, 1 )
df$ongoing <- replace(df$ongoing, czechgo0, 0 )
df$start <- replace(df$start, czechst1, 1 )
df$start <- replace(df$start, czechst0, 0 )
# Ditto for Yugoslavia
yugo <- c("Bosnia and Herzegovina", "The former Yugoslav Republic of Macedonia", "Serbia",
"Montenegro", "Croatia", "Slovenia")
df <- additup(yugo)
yuggo1 <- which( df$year<=1956 &
(df$country=="Bosnia and Herzegovina" | df$country=="The former Yugoslav Republic of Macedonia" |
df$country=="Serbia" | df$country=="Montenegro" | df$country=="Croatia" | df$country=="Slovenia")
)
yuggo0 <- which( df$year>1956 & df$year<1991 &
(df$country=="Bosnia and Herzegovina" | df$country=="The former Yugoslav Republic of Macedonia" |
df$country=="Serbia" | df$country=="Montenegro" | df$country=="Croatia" | df$country=="Slovenia")
)
yugst0 <- which(df$year<1991 &
(df$country=="Bosnia and Herzegovina" | df$country=="The former Yugoslav Republic of Macedonia" |
df$country=="Serbia" | df$country=="Montenegro" | df$country=="Croatia" | df$country=="Slovenia")
)
df$ongoing <- replace(df$ongoing, yuggo1, 1 )
df$ongoing <- replace(df$ongoing, yuggo0, 0 )
df$start <- replace(df$start, yugst0, 0 )
fyuggo1 <- which( df$year>=1998 & df$year<=1999 &
(df$country=="Serbia" | df$country=="Montenegro") )
fyuggo0 <- which( ( (df$year>=1991 & df$year<=1997) | (df$year>=2000 & df$year<=2005) ) &
(df$country=="Serbia" | df$country=="Montenegro") )
fyugst1 <- which( df$year==1998 &
(df$country=="Serbia" | df$country=="Montenegro") )
fyugst0 <- which( ( (df$year>=1991 & df$year<=1997) | (df$year>=1999 & df$year<=2005) ) &
(df$country=="Serbia" | df$country=="Montenegro") )
df$ongoing <- replace(df$ongoing, fyuggo1, 1 )
df$ongoing <- replace(df$ongoing, fyuggo0, 0 )
df$start <- replace(df$start, fyugst1, 1 )
df$start <- replace(df$start, fyugst0, 0 )
# Ethiopia and Eritrea
eritrea <- "Eritrea"
df <- additup(eritrea)
erigo1 <- which( df$year>=1961 & df$year<=1991 & df$country=="Eritrea" )
erigo0 <- which( ( df$year<=1960 | df$year==1992 ) & df$country=="Eritrea" )
erist1 <- which( ( df$year==1961 | df$year==1974 | df$year==1977 ) & df$country=="Eritrea" )
erist0 <- which( ( df$year<=1960 | (df$year>=1962 & df$year<=1973) | (df$year>=1975 & df$year<=1976) |
(df$year>=1978 & df$year<=1992) | (df$year>=1999 & df$year<=2005) ) & df$country=="Eritrea" )
df$ongoing <- replace(df$ongoing, erigo1, 1 )
df$ongoing <- replace(df$ongoing, erigo0, 0 )
df$start <- replace(df$start, erist1, 1 )
df$start <- replace(df$start, erist0, 0 )
# Vietnam
viet <- "Viet Nam"
df <- additup(viet)
df$ongoing <- replace(df$ongoing, which(df$country=="Viet Nam" & df$year>=1954 & df$year<=1975), 1)
df$ongoing <- replace(df$ongoing, which(df$country=="Viet Nam" & df$year<1954), 0)
df$start <- replace(df$start, which(df$country=="Viet Nam" & df$year==1954), 1)
df$start <- replace(df$start, which(df$country=="Viet Nam" & (df$year<1954 | df$year>1954)), 0)
# Yemen
yemen <- "Yemen"
df <- additup(yemen)
df$ongoing <- replace(df$ongoing, which(df$country=="Yemen" & ( ( df$year>=1962 & df$year<=1970) | df$year==1986 ) ), 1)
df$ongoing <- replace(df$ongoing, which(df$country=="Yemen" &
( df$year<1962 | (df$year>1970 & df$year<1986) | (df$year>1986 & df$year<1990) ) ), 0)
df$start <- replace(df$start, which(df$country=="Yemen" & (df$year==1962 | df$year==1986)), 1)
df$start <- replace(df$start, which(df$country=="Yemen" &
( df$year<1962 | (df$year>1962 & df$year<1986) | (df$year>1986 & df$year<1990) ) ), 0)
# Germany
germany <- "Germany"
df <- additup(germany)
df$ongoing <- replace(df$ongoing, which(df$country=="Germany" & df$year < 1990), 0)
df$start <- replace(df$start, which(df$country=="Germany" & df$year < 1990), 0)
# South Sudan
ssudan <- "South Sudan"
df <- additup(ssudan)
df$ongoing <- replace(df$ongoing, which(df$country=="South Sudan" & df$year >= 1956 & df$year < 2011),
df$ongoing[df$country=="Sudan" & df$year >= 1956 & df$year < 2011] )
df$start <- replace(df$start, which(df$country=="South Sudan" & df$year >= 1956 & df$year < 2011),
df$start[df$country=="Sudan" & df$year >= 1956 & df$year < 2011])
# Create 3-level cat var for none, onset, ongoing
df$status <- ifelse(df$ongoing==0, 0,
ifelse(df$start==1, 2, ifelse(is.na(df$start)==FALSE, 1, NA)))
df$status <- factor(df$status, levels=c(0,1,2), labels=c("none", "ongoing", "onset"))
# Function to make a map for a one-year slice
mapit <- function(x) {
jpeg(file=paste("mk", as.character(x), ".jpg", sep=""),
width=5, height=4, units='in', bg='white', res=150, quality=100)
par(mai = c(0, 0, 0.2, 0), xaxs = "i", yaxs = "i")
mapCountryData(joinCountryData2Map(subset(df, year==x), joinCode = "NAME",
nameJoinColumn = "country"),
nameColumnToPlot="status", numCats = 3, catMethod="categorical",
colourPalette=c("gray95", "darkorange", "darkorange4"),
mapTitle=paste(as.character(x)) )
mtext("map made using rworldmap", line=-1, side=1, adj=1, cex=0.5)
dev.off()
}
# Run the function for a selected range of years
for( i in seq(1946,2012,1) ) { mapit(i) }
# Make a .gif.
# NOTE: The code that follows is specific to Windows and requires prior installation of
# ImageMagick (http://www.imagemagick.org/script/index.php). If you install ImageMagick,
# be sure the directory in the first ani.options() line below matches the one where it's
# installed.
library(animation)
ani.options( convert = shQuote("C:/Program Files (x86)/ImageMagick-6.7.6-q16/convert.exe") )
ani.options( outdir = getwd() )
ani.options( nmax = 2012 - 1945)
ani.options( ani.type = "jpeg", ani.dev = "jpeg")
im.convert("mk*.jpg", output = "mkeps19462012.gif")