-
Notifications
You must be signed in to change notification settings - Fork 92
/
choropleth.R
221 lines (176 loc) · 8.11 KB
/
choropleth.R
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
##################################################################
## Source code for the book: "Displaying time series, spatial and
## space-time data with R"
## Copyright (C) 2013-2012 Oscar Perpiñán Lamigueiro
## This program is free software you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation; either version 2 of the License,
## or (at your option) any later version.
## This program is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## General Public License for more details.
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
## 02111-1307, USA.
####################################################################
##################################################################
## Initial configuration
##################################################################
## Clone or download the repository and set the working directory
## with setwd to the folder where the repository is located.
library(lattice)
library(ggplot2)
library(latticeExtra)
myTheme <- custom.theme.2(pch=19, cex=0.7,
region=rev(brewer.pal(9, 'YlOrRd')),
symbol = brewer.pal(n=8, name = "Dark2"))
myTheme$strip.background$col='transparent'
myTheme$strip.shingle$col='transparent'
myTheme$strip.border$col='transparent'
xscale.components.custom <- function(...){
ans <- xscale.components.default(...)
ans$top=FALSE
ans}
yscale.components.custom <- function(...){
ans <- yscale.components.default(...)
ans$right=FALSE
ans}
myArgs <- list(as.table=TRUE,
between=list(x=0.5, y=0.2),
xscale.components = xscale.components.custom,
yscale.components = yscale.components.custom)
defaultArgs <- lattice.options()$default.args
lattice.options(default.theme = myTheme,
default.args = modifyList(defaultArgs, myArgs))
##################################################################
## Choropleth maps
##################################################################
votes2011 <- read.csv('data/votes2011.csv',
colClasses=c('factor', 'factor', 'numeric', 'numeric'))
##################################################################
## Administrative boundaries
##################################################################
library(sp)
library(maptools)
old <- setwd(tempdir())
download.file('http://goo.gl/TIvr4', 'mapas_completo_municipal.rar')
system2('unrar', c('e', 'mapas_completo_municipal.rar'))
espMap <- readShapePoly(fn="esp_muni_0109")
Encoding(levels(espMap$NOMBRE)) <- "latin1"
provinces <- readShapePoly(fn="spain_provinces_ag_2")
setwd(old)
## dissolve repeated polygons
espPols <- unionSpatialPolygons(espMap, espMap$PROVMUN)
## Extract Canarias islands from the SpatialPolygons object
canarias <- sapply(espPols@polygons, function(x)substr(x@ID, 1, 2) %in% c("35", "38"))
peninsulaPols <- espPols[!canarias]
islandPols <- espPols[canarias]
## Shift the island extent box to position them at the bottom right corner
dy <- bbox(peninsulaPols)[2,1] - bbox(islandPols)[2,1]
dx <- bbox(peninsulaPols)[1,2] - bbox(islandPols)[1,2]
islandPols2 <- elide(islandPols, shift=c(dx, dy))
bbIslands <- bbox(islandPols2)
## Bind Peninsula (without islands) with shifted islands
espPols <- rbind(peninsulaPols, islandPols2)
## Match polygons and data using ID slot and PROVMUN column
IDs <- sapply(espPols@polygons, function(x)x@ID)
idx <- match(IDs, votes2011$PROVMUN)
##Places without information
idxNA <- which(is.na(idx))
##Information to be added to the SpatialPolygons object
dat2add <- votes2011[idx, ]
## SpatialPolygonsDataFrame uses row names to match polygons with data
row.names(dat2add) <- IDs
espMapVotes <- SpatialPolygonsDataFrame(espPols, dat2add)
## Drop those places without information
espMapVotes <- espMapVotes[-idxNA, ]
##################################################################
## Map
##################################################################
library(colorspace)
classes <- levels(factor(espMapVotes$whichMax))
nClasses <- length(classes)
qualPal <- rainbow_hcl(nClasses, start=30, end=300)
## distance between hues
step <- 360/nClasses
## hues equally spaced
hue = (30 + step*(seq_len(nClasses)-1))%%360
qualPal <- hcl(hue, c=50, l=70)
pdf(file="figs/whichMax.pdf")
spplot(espMapVotes["whichMax"], col='transparent', col.regions=qualPal)
dev.off()
pdf(file="figs/pcMax.pdf")
quantPal <- rev(heat_hcl(16))
spplot(espMapVotes["pcMax"], col='transparent', col.regions=quantPal)
dev.off()
##################################################################
## Categorical and quantitative variables combined in a multivariate choropleth map
##################################################################
classes <- levels(factor(espMapVotes$whichMax))
nClasses <- length(classes)
step <- 360/nClasses
multiPal <- lapply(1:nClasses, function(i){
rev(sequential_hcl(16, h = (30 + step*(i-1))%%360))
})
pList <- lapply(1:nClasses, function(i){
## Only those polygons corresponding to a level are selected
mapClass <- espMapVotes[espMapVotes$whichMax==classes[i],]
pClass <- spplot(mapClass['pcMax'], col.regions=multiPal[[i]],
col='transparent',
## labels only needed in the last legend
colorkey=(if (i==nClasses) TRUE else list(labels=rep('', 6))),
at = seq(0, 100, by=20))
})
p <- Reduce('+', pList)
## Function to add a title to a legend
addTitle <- function(legend, title){
titleGrob <- textGrob(title, gp=gpar(fontsize=8), hjust=1, vjust=1)
## retrieve the legend from the trellis object
legendGrob <- eval(as.call(c(as.symbol(legend$fun), legend$args)))
## Layout of the legend WITH the title
ly <- grid.layout(ncol=1, nrow=2,
widths=unit(0.9, 'grobwidth', data=legendGrob))
## Create a frame to host the original legend and the title
fg <- frameGrob(ly, name=paste('legendTitle', title, sep='_'))
## Add the grobs to the frame
pg <- packGrob(fg, titleGrob, row=2)
pg <- packGrob(pg, legendGrob, row=1)
}
## Access each trellis object from pList...
for (i in seq_along(classes)){
## extract the legend (automatically created by spplot)...
lg <- pList[[i]]$legend$right
## ... and add the addTitle function to the legend component of each trellis object
pList[[i]]$legend$right <- list(fun='addTitle',
args=list(legend=lg, title=classes[i]))
}
## List of legends
legendList <- lapply(pList, function(x){
lg <- x$legend$right
clKey <- eval(as.call(c(as.symbol(lg$fun), lg$args)))
clKey
})
## Function to pack the list of legends in a unique legend
## Adapted from latticeExtra::: mergedTrellisLegendGrob
packLegend <- function(legendList){
N <- length(legendList)
ly <- grid.layout(nrow = 1, ncol = N)
g <- frameGrob(layout = ly, name = "mergedLegend")
for (i in 1:N) g <- packGrob(g, legendList[[i]], col = i)
g
}
## The legend of p will include all the legends
p$legend$right <- list(fun = 'packLegend', args = list(legendList = legendList))
png(filename="figs/mapLegends.png")
canarias <- provinces$PROV %in% c(35, 38)
peninsulaLines <- provinces[!canarias,]
p +
layer(sp.polygons(peninsulaLines, lwd = 0.1)) +
layer(grid.rect(x=bbIslands[1,1], y=bbIslands[2,1],
width=diff(bbIslands[1,]),
height=diff(bbIslands[2,]),
default.units='native', just=c('left', 'bottom'),
gp=gpar(lwd=0.5, fill='transparent')))
dev.off()