Skip to content

Instantly share code, notes, and snippets.

@briatte
Created April 30, 2013 15:09
Show Gist options
  • Save briatte/5489349 to your computer and use it in GitHub Desktop.
Save briatte/5489349 to your computer and use it in GitHub Desktop.
maps with World Bank and Quality of Government data
require(downloader)
require(foreign)
require(ggplot2)
# download Quality of Government Standard dataset
file = "data/qog-cs.txt"
if(!file.exists(file)) {
if(!file.exists(dta <- "data/qog-cs.dta")) {
url = "http://www.qogdata.pol.gu.se/data/qog_std_cs.dta"
download(url, dta, mode = "wb")
}
write.csv(read.dta(dta), file)
}
# open local copy
data <- read.csv(file, stringsAsFactors = FALSE, header = TRUE)
# extract variables
data <- with(data, data.frame(
region = unique(cname),
TB = tapply(wdi_fr, cname, mean, na.rm = TRUE)
))
# get map data and adjust a few country names
map <- map_data("world")
map$region[map$region == "USA"] <- "United States"
map$region[map$region == "USSR"] <- "Russia"
map <- subset(map, region != "Antarctica")
# merge map and data
data <- merge(map, data, by = "region", all.x = TRUE)
# map options
options <- theme(
text = element_text(size = 12),
panel.border = element_rect(color = "white", fill = NA),
panel.grid = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank(),
legend.position = "bottom"
)
# plot map; check colors() for more funky colors
ggplot(data[order(data$order), ],
aes(y = lat, x = long, group = group, fill = TB)) +
geom_polygon() +
scale_fill_gradient("Birth rate per woman", low = "lightgoldenrod", high = "red") +
labs(x = NULL, y = NULL, title = "Worldwide average fertility rate, 2000-2010") +
theme_bw() + options
require(ggplot2)
require(WDI)
# get WDI indicator
data <- WDI(indicator = "SH.TBS.MORT", start = 2000, end = 2010)
# extract variables
data <- with(data, data.frame(
region = unique(country),
TB = tapply(SH.TBS.MORT, country, mean, na.rm = TRUE)
))
# get map data and adjust a few country names
map <- map_data("world")
map$region[map$region == "USA"] <- "United States"
map$region[map$region == "USSR"] <- "Russian Federation"
map <- subset(map, region != "Antarctica")
# merge map and data
data <- merge(map, data, by = "region", all.x = TRUE)
# map options
options <- theme(
text = element_text(size = 12),
panel.border = element_rect(color = "white", fill = NA),
panel.grid = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank(),
legend.position = "bottom"
)
# plot map
ggplot(data[order(data$order), ],
aes(y = lat, x = long, group = group, fill = TB)) +
geom_polygon() +
scale_fill_gradient("TB death rate per 100,000 people", low = "springgreen3", high = "red") +
labs(x = NULL, y = NULL, title = "Worldwide average tuberculosis death rate, 2000-2010") +
theme_bw() + options
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment