-
Notifications
You must be signed in to change notification settings - Fork 4
/
R11_sf_ggplot2_choropleths.R
206 lines (205 loc) · 6.68 KB
/
R11_sf_ggplot2_choropleths.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
## Maps and choropleths with {ggplot2}
#
library(dplyr)
library(eurostat)
library(sf)
library(ggplot2)
library(RColorBrewer)
rm(list = ls())
#
#
#### Simple maps with {ggplot2}
#
# Get the spatial data for NUTS regions in {sf} format
options(readr.default_locale=readr::locale(tz="Europe/Berlin"))
df60 <- eurostat::get_eurostat_geospatial(resolution = 60)
#
#
# Example of map for Germany - NUTS 2
Germany60 <- df60 %>%
dplyr::filter(LEVL_CODE == 2 & CNTR_CODE %in% ("DE")) %>%
dplyr::select(NUTS_ID)
# ggplot2 map
ggplot() +
geom_sf(data = Germany60)
#
# LAEA geometry
laea = st_crs("+proj=laea +lat_0=51 +lon_0=11")
GermanyLAEA <- st_transform(Germany60,laea)
ggplot() +
geom_sf(data = GermanyLAEA)
#
#
#
#
#### Simple choropleths with {ggplot2}
#
# 1) Read in a dataset
# .. Disposable income of private households by NUTS 2 regions
# http://appsso.eurostat.ec.europa.eu/nui/show.do?dataset=tgs00026
#
Income.DF <- eurostat::get_eurostat("tgs00026", time_format = "num")
summary(Income.DF)
label_eurostat(Income.DF, fix_duplicated = T)[1,]
#
# 2) Filter data for plotting: 2010 & 2015 only, at NUTS2 level
Income <- Income.DF %>%
dplyr::filter(time %in% c(2010,2015), nchar(as.character(geo)) == 4)
# note that the "nchar()" does nothing here as all data are NUTS2 already (ID is 4-digit)
# .. generally, NUTS1 and NUTS0 summarized data would be present in Eurostat datasets
summary(Income)
#
# 3) Merge with {sf} spatial data
# .. if you want LAEA geometry, use map-transformation before joining.
Income.sf <- df60 %>%
dplyr::inner_join(Income, by = c( "NUTS_ID"="geo"))
# .. geo becomes character vector, which is OK
summary(Income.sf) # PPS/Hab for all NUTS2 regions and for two years + shapefiles
#
#
# 4) Plot the data
# We shall use {RColorBrewer} for color-coding
?scale_fill_gradientn
?brewer.pal
# The sequential palettes names are
# Blues BuGn BuPu GnBu Greens Greys Oranges OrRd PuBu
# PuBuGn PuRd Purples RdPu Reds YlGn YlGnBu YlOrBr YlOrRd
# .. from 3 different values up to 9 different values.
# The diverging palettes are
# BrBG PiYG PRGn PuOr RdBu RdGy RdYlBu RdYlGn Spectral
#
#
# 4a) 2015 only, Germany only
Plot1DF <- Income.sf%>%
dplyr::filter(time == 2015 & CNTR_CODE %in% ("DE"))
Plot1DF # we want to plot "values" using the "geometry" entries (on a map)
#
ggplot(Plot1DF) +
geom_sf(aes(fill = values)) +
scale_fill_gradientn('PPS/Hab', colours=brewer.pal(9, "Greens"))+
ggtitle("PPS per Habitant") +
theme_bw()
#
#
# 4b) Plot 2010 & 2015, Germany and Austria
Plot2DF <- Income.sf%>%
dplyr::filter(time %in% c(2010,2015) & CNTR_CODE %in% c("DE","AT"))
head(Plot2DF) # note that data is in "long format"
tail(Plot2DF) # some columns are redundant (geo/id...)
#
ggplot(Plot2DF) +
geom_sf(aes(fill = values)) +
scale_fill_gradientn('PPS/Hab', colours=brewer.pal(9, "Greens"))+
ggtitle("PPS per Habitant") +
facet_wrap(~time, ncol=2)+
theme_bw()
#
#
# 4c) Plot 2015, Spain
Plot3DF <- Income.sf%>%
dplyr::filter(time %in% c(2015) & CNTR_CODE %in% c("ES"))
Plot3DF
#
ggplot(Plot3DF) +
geom_sf(aes(fill = values)) +
scale_fill_gradientn('PPS/Hab', colours=brewer.pal(9, "YlGn"))+
ggtitle("PPS per Habitant") +
theme_dark()
#
# We can avoid plotting Canary islands by limiting x and y coordinates
ggplot(Plot3DF) +
geom_sf(aes(fill = values)) +
scale_fill_gradientn('PPS/Hab', colours=brewer.pal(9, "YlGn"))+
ggtitle("PPS per Habitant") +
coord_sf(xlim = c(-10, 5), ylim = c(35, 45)) # x <-> longitude, y <-> latitude
#
#
# 4d) Plot 2015, Spain & Portugal, add state border-lines
Plot4DF <- Income.sf%>%
dplyr::filter(time %in% c(2010) & CNTR_CODE %in% c("ES","PT"))
Plot4DF
#
borders <- df60 %>%
dplyr::filter(LEVL_CODE == 0 & CNTR_CODE %in% c("ES","PT")) %>%
dplyr::select(NUTS_ID)
#
ggplot() + # note the changed data argument...
geom_sf(data=Plot4DF, aes(fill = values)) + # data for choropleth
scale_fill_gradientn('PPS/Hab', colours=brewer.pal(9, "PuBu"))+ # scale and colors
geom_sf(data=borders, color = "gray30", lwd=1, fill=NA) + # borders - from own DF
labs(title="PPS per Habitant", y="Latitude", x="Longitude")+ # labels
coord_sf(xlim = c(-10, 5), ylim = c(35, 45))+ # map range
theme_light() # theme
#
## Quick exercise 1:
## Repeat previous plot, but switch "Plot4DF" and "borders" line-ordering
#
#
#
## Quick exercise 2:
## Plot a choropleth as follows:
## - Spain and Portugal
## - 2010 and 2015 (use facets - organize in one column)
## - add state borders.
## - use "Greens" palette with 7 levels
#
#
#
#
## Map with inset object (Canary Islands) - a simplified example
#
# Plot4DF repeated (in case "changed" during Q.E.2)
Plot4DF <- Income.sf%>%
dplyr::filter(time %in% c(2010) & CNTR_CODE %in% c("ES","PT"))
borders <- df60 %>%
dplyr::filter(LEVL_CODE == 0 & CNTR_CODE %in% c("ES","PT")) %>%
dplyr::select(NUTS_ID)
#
P1 <- ggplot() +
geom_sf(data=Plot4DF, aes(fill = values)) + # data for choropleth
scale_fill_gradientn('PPS/Hab', colours=brewer.pal(9, "Greens"))+ # scale and colors
geom_sf(data=borders, color = "gray30", lwd=1, fill=NA) + # borders - from own DF
labs(title="PPS per Habitant", y="Latitude", x="Longitude")+ # labels
coord_sf(xlim = c(-11, 5), ylim = c(34, 44.5))+ # map range
theme_light() # theme
#
CI <- ggplot() + # Canary Islands
geom_sf(data=Plot4DF, aes(fill = values)) + # data for choropleth
scale_fill_gradientn(name=NULL, colours=brewer.pal(9, "Greens"))+ # scale and colors
geom_sf(data=borders, color = "gray30", lwd=1, fill=NA) + # borders - from own DF
coord_sf(xlim = c(-18, -13), ylim = c(27.5, 29.5))+ # map range
theme_minimal()+
theme(axis.title = element_blank(), # strip-down the inset plot
axis.text = element_blank(),
axis.ticks = element_blank(),
panel.grid.major = element_line("white"),
legend.position = "none",
plot.background = element_rect(colour = "black"))
#
library(grid)
P1 +
annotation_custom(ggplotGrob(CI), xmin = -12, xmax = -5, ymin = 33.7, ymax = 35.8)
#
#
#
#
#### Simple choropleths with {ggplot2} - binary data
#
# Say, we want to plot the data for Germany, 2015
# and distinguish only above-average from below-average (Germany-wide) values:
#
# 5) 2015 only, Germany only
Plot5DF <- Income.sf%>%
dplyr::filter(time == 2015 & CNTR_CODE %in% ("DE"))
#
meanVal <- mean(Plot5DF$values)
Plot5DF$RichReg <- ifelse(Plot5DF$values >= meanVal,1,0)
#
ggplot(Plot5DF) +
geom_sf(aes(fill = factor(RichReg)))
#
ggplot(Plot5DF) +
geom_sf(aes(fill = RichReg))+
scale_fill_gradientn('PPS/Hab', colours=brewer.pal(3, "Greens"),breaks=NULL)
#
#