-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapping.Rmd
More file actions
154 lines (111 loc) · 5.15 KB
/
mapping.Rmd
File metadata and controls
154 lines (111 loc) · 5.15 KB
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
---
title: "Mapping tools"
author: "Ben Anderson (b.anderson@soton.ac.uk, `@dataknut`)"
date: 'Last run at: `r Sys.time()`'
output:
bookdown::html_document2:
keep_md: no
code_folding: hide
collapsed: no
fig_caption: yes
number_sections: yes
toc: yes
toc_float: yes
subtitle: "Package tests"
bibliography: "statsCodeRefs.bib"
---
```{r knitrSetUp, include=FALSE}
knitr::opts_chunk$set(echo = TRUE) # echo code so reader can see what is happening
knitr::opts_chunk$set(warning = TRUE)
knitr::opts_chunk$set(message = TRUE)
knitr::opts_chunk$set(fig_caption = TRUE)
knitr::opts_chunk$set(fig_height = 6) # default, make it bigger to stretch vertical axis
knitr::opts_chunk$set(fig_width = 8) # full width
knitr::opts_chunk$set(tidy = TRUE) # tidy up code in case echo = TRUE
```
```{r codeSetup, include=FALSE}
# Set start time ----
startTime <- Sys.time()
# Load required packages ----
reqLibs <- c("data.table",
"ggplot2"
)
# do this to install them first if needed
#install.packages(x)
print("Loading required packages")
# be careful - this will return a FALSE if a package doesn't load but the script will NOT stop!
lapply(reqLibs, require, character.only = T)
# local params
myParams <- list()
myParams$dunnersFile <- path.expand("~/Data/NZ_Census/gis/2006/Otago_L2_2006_NZMG_ArcShp/AU_TA_Dunedin_CC.shp") # .shp file for testing boundaries
myParams$imdFile <- path.expand("~/Data/Data/NZ_IMD/gis/Datazones.shp") # Index of Multiple Deprivation File for testing mapping
```
# Why are we here?
To learn how to do mapping in R (markdown). Also uses data.table [@data.table] for data manipulation and ggplot [@ggplot2].
Files we're going to use:
* a .shp file defining the boundaries and data of the NZ [New Zealand Index of Multiple Deprivation (IMD)](https://www.fmhs.auckland.ac.nz/en/soph/about/our-departments/epidemiology-and-biostatistics/research/hgd/research-themes/imd.html) - see [@exeter_new_2017]
# Mapping packages
## ggmap [@ggmap] {.tabset}
Skipped due to google maps API requirement.
## sf [@sf]
See https://r-spatial.github.io/sf/ for documentation
Let's draw a map of the Dunedin district. For those who don't know this is _way_ [bigger](https://en.wikipedia.org/wiki/Dunedin) than the urban area itself - it includes a lot of forests for example!
Figure \@ref(fig:sfBasic) shows two very basic sf-based maps of Dunedin, New Zealand. The first shows just boundaries, the second calculates a test value using qnorm and applies a colour aesthetic to map it.
```{r sfBasic, fig.cap="sf - basic map examples"}
library(sf)
myParams$dunners.shp <- sf::st_read(myParams$dunnersFile)
m <- ggplot2::ggplot() +
geom_sf(data = myParams$dunners.shp)
m
myParams$dunners.shp$testNumeric <- rnorm(n = nrow(myParams$dunners.shp)) # generate some numbers
m <- ggplot2::ggplot() +
geom_sf(data = myParams$dunners.shp,
aes(fill = myParams$dunners.shp$testNumeric)) + # aes works as you'd expect in ggplot
scale_fill_continuous(low = "red", high = "green")
m
```
Next in \@ref(fig:sfMaptile) we try adding a map background using `ggspatial` [@ggspatial]. By default `ggspatial` seems to use Open Street Map. The first one just shows the area unit boundaries. The second shows the test numeric value we calculated before. In each case we use the alpha aesthetic to make the data layer translucent so that we can see the background tile.
```{r sfMaptile, fig.cap="sf - map tile examples"}
library(ggspatial)
myZoom = 10
myAlpha <- 0.3
# will fail if no internet
m <- ggplot2::ggplot() +
annotation_map_tile(zoom = myZoom) + # do this first otherwise it masks the data layers
geom_sf(data = myParams$dunners.shp, aes(alpha = myAlpha)) +
scale_fill_continuous(low = "red", high = "green")
m
m <- ggplot2::ggplot() +
annotation_map_tile(zoom = myZoom) + # do this first otherwise it masks the data layers
geom_sf(data = myParams$dunners.shp, aes(fill = myParams$dunners.shp$testNumeric,
alpha = myAlpha)) +
scale_fill_continuous(low = "red", high = "green")
m
```
## leaflet [@leaflet]
Figure \@ref(fig:leafletBasic) shows where R started :-)
```{r leafletBasic, fig.cap="leaflet - basic plot showing the birthplace of R!"}
library(leaflet)
# will fail if no internet
# uses pipes (doesn't have to)
m <- leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")
m # Print the map
```
How do we do .shp files in leaflet?
## mapview [@mapview]
To do
# About
## Runtime
Analysis completed in: `r round(Sys.time() - startTime, 2)` seconds using [knitr](https://cran.r-project.org/package=knitr) in [RStudio](http://www.rstudio.com) with `r R.version.string` running on `r R.version$platform`.
R packages used (`r reqLibs` plus others loaded where relevant):
* base R - for the basics [@baseR]
* ggmap - maps on ggplot [@ggmap]
* ggplot2 - for slick graphics [@ggplot2]
* ggspatial - to add tile features to ggplot-based maps [@ggspatial]
* knitr - to create this document [@knitr]
* leaflet - leaflet based maps [@leaflet]
* mapview - mapview maps [@mapview]
* sf - maps on ggplot [@sf]
# References