-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
219 lines (168 loc) · 5.94 KB
/
README.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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
set.seed(42)
```
# hpa.spatial <a href='https://healthpolicyanalysis.github.io/hpa.spatial/'><img src='man/figures/hex.png' align="right" style="height:150px"/></a>
<!-- badges: start -->
[![R-CMD-check](https://github.com/healthpolicyanalysis/hpa.spatial/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/healthpolicyanalysis/hpa.spatial/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->
The goal of hpa.spatial is to make relevant shape files and data easily available and include helpful functions for the analysis of spatial data, focusing on the Australian (health) context.
## Notes on other packages
Most shape files are available within [`{absmapsdata}`](https://github.com/wfmackey/absmapsdata) and can be loaded using [`{strayr}`](https://github.com/runapp-aus/strayr).
The way that data are accessed with hpa.spatial both uses these packages as well as replicating their approach to access data from [`{hpa.spatial.data}`](https://github.com/healthpolicyanalysis/hpa.spatial.data).
## Installation
hpa.spatial is only available here on GitHub. You can install the development or release versions of it using the code below:
``` r
# install.packages("remotes")
remotes::install_github("healthpolicyanalysis/hpa.spatial") # development version
remotes::install_github("healthpolicyanalysis/hpa.spatial@*release") # latest release version
```
```{r, message=FALSE, warning=FALSE}
library(hpa.spatial)
library(sf)
library(dplyr)
library(ggplot2)
```
## Getting shapefiles
`get_polygon()` is used to get shape files from the abs.
```{r, warning=FALSE}
sa2_2016 <- get_polygon(area = "sa2", year = 2016)
head(sa2_2016)
sa2_2016 |>
ggplot() +
geom_sf() +
theme_bw() +
ggtitle("SA2 (2016)")
lga_2016 <- get_polygon(area = "lga", year = 2016)
head(lga_2016)
lga_2016 |>
ggplot() +
geom_sf() +
theme_bw() +
ggtitle("LGA (2016)")
```
This is used in the same way as `strayr::read_absmap()` except it also includes a `simplify_keep` argument for simplifying the polygon.
```{r, eval=FALSE}
sa2_2016_simple <- get_polygon(area = "sa2", year = 2016, simplify_keep = 0.1)
sa2_2016 |>
filter(gcc_name_2016 == "Greater Brisbane") |>
ggplot() +
geom_sf() +
scale_x_continuous(limits = c(152.9, 153.1)) +
scale_y_continuous(limits = c(-27.4, -27.6)) +
theme_bw()
sa2_2016_simple |>
filter(gcc_name_2016 == "Greater Brisbane") |>
ggplot() +
geom_sf() +
scale_x_continuous(limits = c(152.9, 153.1)) +
scale_y_continuous(limits = c(-27.4, -27.6)) +
theme_bw()
```
```{r, echo=FALSE, message=FALSE, warning=FALSE}
sa2_2016_simple <- get_polygon(area = "sa2", year = 2016, simplify_keep = 0.1)
p1 <- sa2_2016 |>
filter(gcc_name_2016 == "Greater Brisbane") |>
ggplot() +
geom_sf() +
scale_x_continuous(limits = c(152.9, 153.1)) +
scale_y_continuous(limits = c(-27.4, -27.6)) +
theme_bw()
p2 <- sa2_2016_simple |>
filter(gcc_name_2016 == "Greater Brisbane") |>
ggplot() +
geom_sf() +
scale_x_continuous(limits = c(152.9, 153.1)) +
scale_y_continuous(limits = c(-27.4, -27.6)) +
theme_bw()
gridExtra::grid.arrange(p1, p2, ncol = 2)
```
### Other shapefiles
Aside from the built in shapefiles that are hosted by `{absmapsdata}`, `get_polygon()` can also access shapefiles for local hospital networks (LHNs) and Primary Health Networks (PHNs).
For example, these can be accessed by the `"area"` or `"name"` arguments as "LHN".
```{r, warning=FALSE}
qld_hhs <- get_polygon(area = "LHN") |> filter(state == "QLD")
head(qld_hhs)
qld_hhs |>
ggplot() +
geom_sf() +
theme_bw()
```
## Mapping data between ASGS editions
`map_data_with_correspondence()` is used to map data across ASGS editions.
When used with unit level data, it will randomly allocate the value to the code of the updated edition based on the population-weighted proportions (as probabilities) on the relevant correspondence table.
```{r}
map_data_with_correspondence(
codes = c(107011130, 107041149),
values = c(10, 10),
from_area = "sa2",
from_year = 2011,
to_area = "sa2",
to_year = 2016,
value_type = "units"
)
```
When used with aggregate data, it will split the value among the codes of the updated edition based on the population-weighted proportions on the relevant correspondence table.
```{r}
map_data_with_correspondence(
codes = c(107011130, 107041149),
values = c(10, 10),
from_area = "sa2",
from_year = 2011,
to_area = "sa2",
to_year = 2016,
value_type = "aggs"
)
```
### Example
Suppose we have counts of patients within SA2s (2011) and we want to aggregate these up into SA3s (2016 edition). Here is how we could do this with `map_data_correspondence()` mapping up both to a higher level of ASGS and to a more recent edition.
```{r, warning=FALSE, message=FALSE}
sa2_2011 <- get_polygon("sa22011")
sa2_2011$patient_counts <- rpois(n = nrow(sa2_2011), lambda = 30)
sa3_counts <- map_data_with_correspondence(
codes = sa2_2011$sa2_code_2011,
values = sa2_2011$patient_counts,
from_area = "sa2",
from_year = 2011,
to_area = "sa3",
to_year = 2016,
value_type = "aggs"
) |>
rename(patient_counts = values)
sa3_2016 <- get_polygon("sa32016") |>
left_join(sa3_counts)
```
```{r, eval=FALSE}
sa2_2011 |>
ggplot() +
geom_sf(aes(fill = patient_counts)) +
ggtitle("Patient counts by SA2 (2011)") +
theme_bw()
sa3_2016 |>
ggplot() +
geom_sf(aes(fill = patient_counts)) +
ggtitle("Patient counts by SA3 (2016)") +
theme_bw()
```
```{r, echo=FALSE, message=FALSE, warning=FALSE}
p1 <- sa2_2011 |>
ggplot() +
geom_sf(aes(fill = patient_counts)) +
ggtitle("Patient counts by SA2 (2011)") +
theme_bw()
p2 <- sa3_2016 |>
ggplot() +
geom_sf(aes(fill = patient_counts)) +
ggtitle("Patient counts by SA3 (2016)") +
theme_bw()
gridExtra::grid.arrange(p1, p2, ncol = 1)
```