These data come from the University of Maryland Global Terrorism Database [http://www.start.umd.edu/gtd/]. The data span 1970 to 2013; each record is a different terrorist incident. In what follows we will construct a heatmap for each of the 44 years in the database.
library(sp) # add spatial package to load the S4 spatial objects
library(spatstat) # to calculate field of point density
##
## spatstat 1.37-0 (nickname: 'Model Prisoner')
## For an introduction to spatstat, type 'beginner'
library(rworldmap) # to obtain countriesCoarse map file
## ### Welcome to rworldmap ###
## For a short introduction type : vignette('rworldmap')
Country boundaries are from an internal R dataset. The terrorism data are downloaded from my website.
data(countriesCoarse) #world country boundaries
load(url("http://capone.mtsu.edu/eaeff/downloads/GDB19702013.Rdata")) # contains a dataframe named "u" of terrorist incidents
head(u,2) #look at top two rows of dataframe
## iyear country_txt city latitude longitude
## 1 1970 Dominican Republic Santo Domingo 18.46 -69.95
## 2 1970 Mexico Mexico city 19.43 -99.13
## attacktype1_txt targtype1_txt
## 1 Assassination Private Citizens & Property
## 2 Hostage Taking (Kidnapping) Government (Diplomatic)
## gname weapdetail nkill
## 1 MANO-D 1
## 2 23rd of September Communist League 0
We plot the frequency of terrorist incidents by year: note the increase since the Iraq invasion.
plot(table(u$iyear),ylab="frequency of terrorist incidents") #shows frequency of terrorist incidents by year
We will loop through each year of the data, each time calculating density of incidents, and writing our maps to *.png format files.
for (ii in unique(u$iyear)){
png(file=paste("y",ii,".png",sep=""),width=8,height=5,units="in",pointsize=10,res=500)
sSp <- as(SpatialPoints(u[which(u$iyear==ii),c("longitude","latitude")]), "ppp") # convert points to ppp class
Dens <- density(sSp, adjust = 0.2) # create density object
Dens$v[which(Dens$v<=quantile(Dens$v,.8))]<-NA # deletes lowest values in density image
x<-range(Dens$v,na.rm=TRUE)
tc <- colourmap(rainbow(128), breaks=seq(from=x[1],to=x[2],length=129))
plot(Dens, col=tc,box=FALSE, main=paste("Terrorist incidents",ii))
plot(countriesCoarse,add=TRUE)
dev.off()
}
getwd() # look for the *.png files in this directory
## [1] "E:/htm"
Here is an example of what our maps look like: