-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy path100_maps.Rmd
executable file
·115 lines (99 loc) · 5.89 KB
/
100_maps.Rmd
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
<style>@import url(style.css);</style>
[Introduction to Data Analysis](index.html "Course index")
# 10. Visualization in space: Maps
Let's stick some data on a map, using a [simple plotting method][ds-map] that can easily match country-level data to world map coordinates. We will come to [using raw geodata][okfn-maps] in a minute, but for now we will just concentrate on showing how to map a simple cross-sectional data structure like the ones that we have been using in previous sessions.
[ds-map]: http://is-r.tumblr.com/post/35200999886/make-your-own-electoral-map "Make your own electoral map (David Sparks)"
[okfn-maps]: http://schoolofdata.org/2013/11/09/web-mapping/ "So you want to make a map… (Noah Veltman, School of Data)"
```{r packages, message = FALSE, warning = FALSE}
# Load packages.
packages <- c("countrycode", "downloader", "foreign", "ggplot2", "maps", "RColorBrewer")
packages <- lapply(packages, FUN = function(x) {
if(!require(x, character.only = TRUE)) {
install.packages(x)
library(x, character.only = TRUE)
}
})
```
We will use [Quality of Government][qog] data, for which the country codes are standardized, and a rather old map of the world where a few countries need to be renamed to be matched to QOG country names. The most ambiguous case is that we will be using USSR borders as approximate frontiers for Russia, thereby losing the opportunity to observe post-Soviet states on the final map.
[qog]: http://www.qog.pol.gu.se/
```{r qog-data}
# Get world map coordinates from the maps package.
w <- map_data("world", zoom = 4)
# Fix a few country names.
w$region[which(w$region == "USA")] = "United States"
w$region[which(w$region == "UK")] = "United Kingdom"
w$region[which(w$region == "USSR")] = "Russia"
w$region[which(w$region == "Zaire")] = "Congo, Democratic Republic"
w$region[which(w$region == "North Korea")] = "Korea, North"
w$region[which(w$region == "South Korea")] = "Korea, South"
# Remove Antarctica.
w <- subset(w, !region %in% c("Antarctica", "Greenland"))
# Download Quality of Government Standard dataset.
zip = "data/qog.cs.zip"
qog = "data/qog.cs.csv"
if(!file.exists(zip)) {
dta = "data/qog.cs.dta"
download("http://www.qogdata.pol.gu.se/data/qog_std_cs.dta", dta, mode = "wb")
write.csv(read.dta(dta, warn.missing.labels = FALSE), qog)
zip(zip, file = c(dta, qog))
file.remove(dta, qog)
}
qog = read.csv(unz(zip, qog), stringsAsFactors = FALSE)
```
Here's a quick function to plot Quality of Government data. The first part extracts a variable based on the argument passed to the function, along with (cleaned up) country names. The second part matches this data to the map, based on the `region` identifier of the world map data. The ast part plots the data by longitude and latitude, using a 'whitened' `ggplot2` theme.
```{r qog-map, tidy = FALSE}
qog.map <- function(x) {
# Extract QOG variables.
data <- with(qog, data.frame(
country = gsub(" (\\(.*)", "", cname),
variable = qog[, which(names(qog) == x)]))
# Match QOG data to map.
w$fill <- with(data, by(variable, country, mean))[w$region]
set1 = brewer.pal(9, "Set1")
# Plot over blank theme.
p = qplot(data = w, x = long, y = lat,
group = group, fill = fill, geom = "polygon")
p = p + scale_fill_gradient2("",
low = set1[1], mid = set1[6], high = set1[3],
midpoint = mean(w$fill, na.rm = TRUE),
na.value = set1[9])
p = p + theme(axis.text = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank())
p = p + labs(y = NULL, x = NULL)
return(p)
}
```
You can pass any QOG variable name to the function, although its matching method will only support continuous variables, as shown below with an additive index of human rights by [Cingranelli and Richards][ciri]. The default color [gradient][ggplot2-gradient] uses [ColorBrewer][cb]'s `Set1` scheme and goes from green (low) to yellow (at midpoint) to red (high) by default.
[ciri]: http://www.humanrightsdata.org/
[cb]: http://colorbrewer2.org/ "ColorBrewer 2.0 (Cynthia Brewer)"
```{r qog-map-example-1-auto, fig.width = 12, fig.height = 6.6, tidy = FALSE}
# Plot a simple QOG map.
qog.map("ciri_empinx_new") +
labs(title = "CIRI Empowerment Rights Index (0-14) in 2009")
```
You can bypass the function's colors by overriding its `ggplot2` fill scale. The example below uses a color gradient of [Crayola colors][sss-crayola] that were coded for R by Matt Blackwell (check out Harvard's Social Science Statistics blog for other goodies, like this [elementary Google Maps][sss-googlemap] function by Andy Eggers). The example variable will show a clear outlier in its worldwide distribution.
[ggplot2-gradient]: http://docs.ggplot2.org/current/scale_gradient.html
[sss-crayola]: http://blogs.iq.harvard.edu/sss/archives/2011/02/crayola_colors.shtml
[sss-googlemap]: http://blogs.iq.harvard.edu/sss/archives/2008/04/google_charts_f_1.shtml
```{r qog-map-example-2-auto, fig.width = 12, fig.height = 6.6, tidy = FALSE, message = FALSE}
# Get Crayola colors from a personal copy.
url = "https://gist.github.com/briatte/5813759/raw/53edb4d59e1c54a2abc6c4c034ef4925625bc8cb/crayola.R"
source_url(url, prompt = FALSE)
# Get a variable's mean.
the_mean = mean(qog$wdi_the, na.rm = TRUE)
# Get a variable's quantiles.
the_quantiles = as.vector(round(quantile(qog$wdi_the, na.rm = TRUE)))
# Plot with Crayola color gradient.
qog.map("wdi_the") +
labs(title = "Total health expenditure (% of GDP) in 2009") +
scale_fill_gradient2("",
low = crayola["Blue Green"],
high = crayola["Sunset Orange"],
mid = crayola["Yellow"],
midpoint = the_mean,
limits = range(the_quantiles),
breaks = the_quantiles,
na.value = crayola["Gray"])
```
> __Next__: [Geocoding](101_geocoding.html).