-
Notifications
You must be signed in to change notification settings - Fork 0
/
rMappingExample.Rmd
278 lines (195 loc) · 8.71 KB
/
rMappingExample.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
---
title: "Maps in R"
subtitle: "A very simple introduction"
author: "Ben Anderson & Tom Rushby (ECCD)"
date: 'Last run at: `r Sys.time()`'
output:
bookdown::pdf_document2:
toc: yes
fig_caption: yes
number_sections: yes
bookdown::html_document2:
fig_caption: yes
code_folding: hide
number_sections: yes
toc: yes
toc_depth: 4
toc_float: TRUE
bookdown::word_document2:
fig_caption: yes
number_sections: yes
toc: yes
toc_depth: 4
fig_width: 5
always_allow_html: yes
---
```{r setup, include=FALSE}
# you might need to install these first
# install.packages("sf")
# install.packages("raster")
# install.packages("spData")
# remotes::install_github("Nowosad/spDataLarge")
library(sf) # classes and functions for vector data
library(raster) # classes and functions for raster data
library(spData) # load geographic data
library(spDataLarge) # load larger geographic data
```
# R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunks
We like R Markdown. In this file we step through some very simple mapping functions in R/Rmd.
# Geo-comp
Doing maps and stuff in R.
We use https://geocompr.robinlovelace.net/ a lot...
# Explore 'World' maps
First try using the `sf` [package](https://cran.r-project.org/web/packages/sf/index.html)'s built-in `world` data to make a simple map plot.
It's worth remembering that a map is just a plot with spatial arrangements...
```{r world_maps}
head(world)
# plot variables 3 to 6
plot(world[3:6])
# plot world population
plot(world["pop"])
```
Now re-draw the population map using `ggplot2` and the `geom_sf` geometry... This will make a much [prettier map](https://www.johan-rosa.com/2019/11/13/creating-maps-with-sf-and-ggplot2/) because `ggplot2` is so cool.
```{r world_maps_ggplot}
# using ggplot
library(ggplot2)
ggplot2::ggplot(world) +
geom_sf(aes(fill = pop/1000000)) +
scale_fill_continuous(name="Population (millions)",
low = "green",
high = "red")
```
That looks better...well, if we sorted out the colours and the visual dominance of 2 countries...
# Local Authorities
## Loading data
Now we're going to load some polygon boundary data (you need internet access for this) and some energy demand data so we can link them and map them.
```{r loadLAElecData}
library(readr)
# electricity consumption data at LA level
la_elecData <- readr::read_csv("https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/946424/Subnational_electricity_consumption_statistics_2019.csv")
```
This is electricity consumption data for 2019 for English Local Authorities. What variables have we got?
```{r elecLAVars}
head(la_elecData)
```
Now load the boundary data - we will use Local Authority boundaries for the Solent region only to keep things small & easy to play with.
```{r loadLABoundaries}
# las_to_load <- c("Southampton","Portsmouth","Winchester",
# "Eastleigh","Isle of Wight","Fareham",
# "Gosport","Test Valley","East Hampshire",
# "Havant","New Forest","Hart","Basingstoke and Deane")
# we pre-saved this data in the data folder of the repo for speed
# see getBoundaryData.R for how it works
inf <- here::here("data", "boundaries", "la_solent.shp") # use here to specify the data location
message("Loading LA geometry from ONS Open Geography API")
la_sf_data <- sf::read_sf(inf)
head(la_sf_data)
```
Which areas have we got?
```{r boundaryLAAreas}
table(la_sf_data$lad18nm)
```
## Mapping data
Now we'll map/plot some of the data using the `ggplot2` approach. To do that we need to merge the boundaries and the energy data so that we can `fill` the boundaries with a colour according to one of the variables.
```{r plotLAElec}
# create a variable with the LA code and the same name as in the sf_data
la_elecData$lad18cd <- la_elecData$`LA Code`
# merge them
la_merged_sf <- merge(la_sf_data, la_elecData)
# plot
ggplot2::ggplot(la_merged_sf) +
geom_sf(aes(fill = Total_GWh)) +
scale_fill_continuous(name = "Total GWh", low = "green", high = "red")
```
# MSOAs
These are census areas - smaller than LAs so the files are bigger and the maps are denser.
## Loading data
As before we're going to load some polygon boundary data (you need internet access for this) and some energy demand data so we can link them and map them.
```{r loadElecMSOAData}
# electricity consumption data at MSOA level (pre downloaded)
inFile <- here::here("data", "energy", "MSOA_Dom_Elec", "MSOA_DOM_ELEC_2019.csv")
msoa_elecData <- readr::read_csv(inFile)
```
This is electricity consumption data for 2019 for MSOAs. What variables have we got?
```{r elecMSOAVars}
head(msoa_elecData)
```
Clearly we are going to need to filter out the ones we don;t want...
Now load the MSOA boundary data for the Solent region only to keep things small & easy to play with.
We pre-downloaded these.
```{r loadMSOABoundaries}
# las_to_load <- c("Southampton","Portsmouth","Winchester",
# "Eastleigh","Isle of Wight","Fareham",
# "Gosport","Test Valley","East Hampshire",
# "Havant","New Forest","Hart","Basingstoke and Deane")
# we pre-saved this data in the data folder of the repo for speed
# see getBoundaryData.R for how it works
inf <- here::here("data", "boundaries", "msoa_solent.shp") # use here to specify the data location
message("Loading MSOA geometry from ONS Open Geography API")
msoa_sf_data <- sf::read_sf(inf)
head(msoa_sf_data)
```
Which areas have we got and how many MSOAs are there in each?
```{r boundaryMSOAAreas}
table(msoa_sf_data$LAD11NM)
```
## Mapping data
Now we'll map/plot some of the data using the `ggplot2` approach. To do that we need to merge the boundaries and the energy data so that we can `fill` the boundaries with a colour according to one of the variables.
```{r plotMSOAElec}
# create a variable with the LA code and the same name as in the sf_data
msoa_elecData$MSOA11CD <- msoa_elecData$`Middle Layer Super Output Area (MSOA) Code`
# merge them
msoa_merged_sf <- merge(msoa_sf_data, msoa_elecData)
# plot
ggplot2::ggplot(msoa_merged_sf) +
geom_sf(aes(fill = `Mean consumption (kWh per meter)`)) +
scale_fill_continuous(name = "Mean kWh per meter", low = "green", high = "red") +
labs(caption = "Solent (all MSOAs)")
```
# LSOAs
Check this works with BEIS LSOA level electricity data.
```{r loadLSOAElecData}
inFile <- here::here("data", "energy", "LSOA_Dom_Elec", "LSOA_ELEC_2019.csv")
lsoa_elecData <- readr::read_csv(inFile)
lsoa_elecData$LSOA11CD <- lsoa_elecData$`Lower Layer Super Output Area (LSOA) Code`
```
```{r loadLSOABoundaries}
# las_to_load <- c("Southampton","Portsmouth","Winchester",
# "Eastleigh","Isle of Wight","Fareham",
# "Gosport","Test Valley","East Hampshire",
# "Havant","New Forest","Hart","Basingstoke and Deane")
# we pre-saved this data in the data folder of the repo for speed
# sourced from: https://geoportal.statistics.gov.uk/search?collection=Dataset&sort=name&tags=all(BDY_LSOA%2CDEC_2011)
# see getBoundaryData.R for how it works
inf <- here::here("data", "boundaries", "lsoa_solent.shp") # use here to specify the data location
message("Loading LSOA geometry from file")
lsoa_sf_data <- sf::read_sf(inf)
head(lsoa_sf_data)
```
Which areas have we got and how many MSOAs are there in each?
```{r boundaryLSOAAreas}
table(lsoa_sf_data$LAD11NM)
```
## Mapping data
Now we'll map/plot some of the data using the `ggplot2` approach. To do that we need to merge the boundaries and the energy data so that we can `fill` the boundaries with a colour according to one of the variables.
```{r plotLSOAElec}
lsoa_merged_sf <- merge(lsoa_sf_data, lsoa_elecData)
# plot
ggplot2::ggplot(lsoa_merged_sf) +
geom_sf(aes(fill = `Mean domestic electricity consumption \n(kWh per meter)`)) +
scale_fill_continuous(name = "Mean kWh per meter", low = "green", high = "red") +
labs(caption = "Solent (all LSOAs)")
```
Cities disappear due to the density of boundaries. As an example, this is the map for just Southampton.
```{r plotLSOAElecSoton}
library(dplyr) # for filter
mapData <- dplyr::filter(lsoa_merged_sf, LAD11NM == "Southampton")
# plot
ggplot2::ggplot(mapData) +
geom_sf(aes(fill = `Mean domestic electricity consumption \n(kWh per meter)`)) +
scale_fill_continuous(name = "Mean kWh per meter", low = "green", high = "red") +
labs(caption = "Southampton")
```
# References