-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06 - Maps.Rmd
155 lines (124 loc) · 3.44 KB
/
06 - Maps.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
---
title: 'ggplot2 Elegant Graphics: Chapter 6'
author: "Brandon Foltz"
date: "2023-03-12"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(ozmaps)
library(sf)
```
```{r}
mi_counties <- map_data("county", "michigan")|>
select(lon = long, lat, group, id = subregion)
head(mi_counties)
ggplot(mi_counties,
aes(lon, lat)) +
geom_point(size = .25, show.legend = FALSE) +
coord_quickmap()
ggplot(mi_counties,
aes(lon, lat, group = group)) +
geom_polygon(fill = "white", color = "grey50") +
coord_quickmap()
```
Simple Features
```{r}
oz_states <- ozmaps::ozmap_states
oz_states
ggplot(oz_states) +
geom_sf() +
coord_sf()
```
Layered Maps
```{r}
oz_states <- ozmaps::ozmap_states|>
filter(NAME != "Other Territories")
oz_votes <- rmapshaper::ms_simplify(ozmaps::abs_ced)
ggplot() +
geom_sf(data = oz_states, mapping = aes(fill = NAME), show.legend = FALSE) +
geom_sf(data = oz_votes, fill = NA) +
coord_sf()
```
Labelled Maps
```{r}
sydney_map <- ozmaps::abs_ced %>% filter(NAME %in% c(
"Sydney", "Wentworth", "Warringah", "Kingsford Smith", "Grayndler", "Lowe",
"North Sydney", "Barton", "Bradfield", "Banks", "Blaxland", "Reid",
"Watson", "Fowler", "Werriwa", "Prospect", "Parramatta", "Bennelong",
"Mackellar", "Greenway", "Mitchell", "Chifley", "McMahon"
))
ggplot(sydney_map) +
geom_sf(aes(fill = NAME), show.legend = FALSE) +
coord_sf(xlim = c(150.97, 151.3), ylim = c(-33.98, -33.79)) +
geom_sf_label(aes(label = NAME), label.padding = unit(1, "mm"))
```
Adding other geoms
```{r}
oz_capitals <- tibble::tribble(
~city, ~lat, ~lon,
"Sydney", -33.8688, 151.2093,
"Melbourne", -37.8136, 144.9631,
"Brisbane", -27.4698, 153.0251,
"Adelaide", -34.9285, 138.6007,
"Perth", -31.9505, 115.8605,
"Hobart", -42.8821, 147.3272,
"Canberra", -35.2809, 149.1300,
"Darwin", -12.4634, 130.8456,
)
ggplot() +
geom_sf(data = oz_votes) +
geom_sf(data = oz_states, color = "black", fill = NA) +
geom_point(data = oz_capitals, mapping = aes(x = lon, y = lat), color = "red") +
coord_sf()
```
```{r}
ggplot(oz_votes) + geom_sf()
ggplot(oz_votes) + geom_sf() + coord_sf(crs = st_crs(3112))
```
Working with sf data
```{r}
edenmonaro <- ozmaps::abs_ced|>
filter(NAME == "Eden-Monaro")
p <- ggplot(edenmonaro) + geom_sf()
p + coord_sf(xlim = c(147.75, 150.25), ylim = c(-37.5, -34.5))
p + coord_sf(xlim = c(150, 150.25), ylim = c(-36.3, -36))
edenmonaro <- edenmonaro|>
pull(geometry)
st_bbox(edenmonaro)
edenmonaro
st_cast(edenmonaro, "POLYGON")
```
```{r}
dawson <- ozmaps::abs_ced|>
filter(NAME == "Dawson")|>
pull(geometry)
dawson
ggplot(dawson) +
geom_sf() +
coord_sf()
# Find only the islands
# find mainland
dawson <- st_cast(dawson, "POLYGON")
which.max(st_area(dawson))
# remove mainland to get islands
ggplot(dawson[-69]) +
geom_sf() +
coord_sf()
```
Raster Images
```{r}
# list of all file names with time stamp 2020-01-07 21:00 GMT
# (BOM images are retained for 24 hours, so this will return an
# empty vector if you run this code without editing the time stamp)
files <- bomrang::get_available_imagery() %>%
stringr::str_subset("202001072100")
# use curl_download() to obtain a single file, and purrr to
# vectorise this operation
purrr::walk2(
.x = paste0("ftp://ftp.bom.gov.au/anon/gen/gms/", files),
.y = file.path("raster", files),
.f = ~ download.file(url = .x, destfile = .y)
)
```