library(sp)         # library for spatial data objects in R
  library(rgdal)      # Geospatial Data Abstraction Library
  library(rgeos)      # Geometry Engine
  library(raster)     # raster-based spatial operations
  library(cshapes)    # library with polygons of country borders
  library(ggmap)      # tools to access Google Maps
  library(sna)        # social network analysis tools, incl gplot

1 Opening data

We continue with the data set for Project 2. The primary data set is replication data from Erasmus Kersting and Christopher Kily (2014), “Aid and democracy redux”, European Economic Review. We also incorporate the code from three weeks ago to generate new variables.

aid <- import("")

The code for merging and computing variables is hidden, but available in previous labs.

2 Open borders data

2.1 Using cshapes

The cshapes package is a library with data on the locations of country borders across the world, that can be queried for specific dates. Here we extract the map for 2012.

borders <- cshp(date = as.Date("2012-01-01"))
## Warning: use rgdal::readOGR or sf::st_read
polity2 <- p4$P4_polity2
names(polity2) <- p4$P4_ccode
names(polity2)[p4$P4_country == "South Sudan"] <- 625
names(polity2)[p4$P4_country == "Ethiopia"] <- 530
borders@data$polity <- polity2[as.character(borders@data$COWCODE)]

demLevel <- 1 - (borders@data$polity + 11) / 22
plot(borders, col = gray(ifelse(, 1, demLevel)), main = "Worldwide democracy")

The merging of the shape data with the democracy data above is a bit clumsy, because a merge command can change the order of the data set, which is problematic, as it would lose the mapping of the data points to the correct country borders.

We can use limits in the plot command to extract only a subset of the map, here the African continent.

plot(borders, col = gray(ifelse(, 1, demLevel)), main = "African democracy", xlim = c(-18, 55), ylim = c(-36, 39))