Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Global Slavery Rates (from Global Slavery & US State Department)
SlaveryData <- read.csv("data/global_slavery_index_2013-transformed.csv")
library(ggplot2)
library(rgeos)
library(maptools)
library(extrafont)
# font_import() only needs to be done once and I've already done it
loadfonts(device = "win")
map.world <- readShapeSpatial("maps/world/ne_110m_admin_0_countries.shp")
plot(map.world)
# I'm a dummy who used the sov_a3 region rather than the adm0_a3 region;
# whelp, my ISO_A3 tags in the dataset are not accurate
map.world <- fortify(map.world, region = "sov_a3")
map.world$id <- toupper(map.world$id)
# Bahrain (BHR), Barbados (BRB), Cape Verde (CPV)
# Hong Kong (HKP), Mauritius (MUS), Singapore (SGP)
# not present in the map, but are in the data set
# all fairly small countries which do not destroy the map
# If you want to verify what doesn't match between the map and the dataset then
# namesInData <- levels(factor(SlaveryData$ISO_A3))
# namesInMap <- levels(factor(map.world$id))
# namesInData[which(!namesInData %in% namesInMap)]
# namesInMap[which(!namesInMap %in% namesInData)]
p1 <- ggplot()
p1 <- p1 + geom_map(data = SlaveryData, aes(map_id = ISO_A3, fill = USATIPS), colour = "gray40", lwd = 0.1, map = map.world, alpha=.8)
p1 <- p1 + expand_limits(x = map.world$long, y = map.world$lat)
p1 <- p1 + theme(panel.background = element_rect(fill = "white"), line = element_blank())
p1 <- p1 + scale_fill_gradient2(low = "dodgerblue2", mid = "tan1", midpoint = 2, high = "firebrick1", limits = c(1, 3))
p1 <- p1 + theme(legend.title=element_text(size=12)) + theme(legend.title.align=.5)
p1 <- p1 + theme(legend.text=element_text(family = "HelveticaNeueLT Com 57 Cn", size = 10))
p1 <- p1 + theme(text=element_text(family="Helvetica World", size=12))
p1 <- p1 + theme(legend.justification=c(0,0), legend.position=c(0,0))
p1 <- p1 + theme(plot.title = element_text(size = rel(2), family = "Helvetica World"))
print(p1)
SlaveryData <- read.csv("data/global_slavery_index_2013-transformed.csv")
# Excel was a bad boy and added commas to the population and enslaved figures
# so gotta remove those, bitches
pop <- gsub(",","",SlaveryData$Population)
# coerce the population into numeric
pop <- as.numeric(pop)
slaves <- gsub(",","",SlaveryData$EnslavedMean)
slaves <- as.numeric(slaves)
# create a log rate of slavery in a country figure
rate <- (log(slaves)/(log(pop)))
library(ggplot2)
library(rgeos)
library(maptools)
library(extrafont)
# font_import() only needs to be done once and I've already done it
loadfonts(device = "win")
map.world <- readShapeSpatial("maps/world/ne_110m_admin_0_countries.shp")
plot(map.world)
# I'm a dummy who used the sov_a3 region rather than the adm0_a3 region;
# whelp, my ISO_A3 tags in the dataset are not accurate
map.world <- fortify(map.world, region = "sov_a3")
map.world$id <- toupper(map.world$id)
# Bahrain (BHR), Barbados (BRB), Cape Verde (CPV)
# Hong Kong (HKP), Mauritius (MUS), Singapore (SGP)
# not present in the map, but are in the data set
# all fairly small countries which do not destroy the map
# If you want to verify what doesn't match between the map and the dataset then
# namesInData <- levels(factor(SlaveryData$ISO_A3))
# namesInMap <- levels(factor(map.world$id))
# namesInData[which(!namesInData %in% namesInMap)]
# namesInMap[which(!namesInMap %in% namesInData)]
p1 <- ggplot()
p1 <- p1 + geom_map(data = SlaveryData, aes(map_id = ISO_A3, fill = rate), colour = "gray40", lwd = 0.7, map = map.world, alpha = 0.9)
p1 <- p1 + expand_limits(x = map.world$long, y = map.world$lat)
p1 <- p1 + labs(title = expression("Slavery Rates"))
p1 <- p1 + theme(panel.background = element_rect(fill = "white"), line = element_blank())
p1 <- p1 + scale_fill_gradient2(low = "dodgerblue2", mid = "white", midpoint = 0.6, high = "firebrick1", limits = c(0.23, 0.79), name = "Percent of \nPopulation who\nare enslaved")
p1 <- p1 + theme(legend.title=element_text(size=12)) + theme(legend.title.align=.5)
p1 <- p1 + theme(legend.text=element_text(family = "HelveticaNeueLT Com 57 Cn", size = 10))
p1 <- p1 + theme(text=element_text(family="Helvetica World", size=12))
p1 <- p1 + theme(legend.justification=c(0,0), legend.position=c(0,0))
p1 <- p1 + theme(plot.title = element_text(size = rel(2), family = "Helvetica World"))
print(p1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment