-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.R
168 lines (142 loc) · 5.27 KB
/
map.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
library(sf)
# Custom Oblique Mercator projection
projection <- st_crs("+proj=omerc +lat_0=40 +lonc=-74 +gamma=-40")
# RTWB detections
rtwb <- st_read('data/rtwb_detections.gpkg',
quiet = TRUE) |>
st_transform(projection)
# Coastline
coastline <- st_read('data/spatial/coastline.gpkg',
quiet = TRUE) |>
st_make_valid() |>
# Crop to eastern seaboard
st_intersection(
st_bbox(c(xmin = -80, xmax = -50,
ymin = 30, ymax = 50), crs = 4326) |>
st_as_sfc()
) |>
st_transform(projection)
# NARW critical habitat, USA
narw_usa <- st_read('data/spatial/narw_usa.gpkg',
quiet = TRUE) |>
st_transform(projection)
# NARW critical habitat, CAN
narw_can <- st_read('data/spatial/narw_can.gpkg',
quiet = TRUE) |>
st_transform(projection)
# NARW SMA
narw_sma <- st_read('data/spatial/narw_sma.gpkg',
quiet = TRUE) |>
st_transform(projection)
library(ggplot2)
library(patchwork)
yesterday <- rtwb |>
filter(Date == Sys.Date() - 1)
day_before <- rtwb |>
filter(Date == Sys.Date() - 2)
flyway_plot <- function(data_subset){
narw_plot <- ggplot() +
geom_sf(data = narw_usa, fill = 'pink', color = NA, alpha = 0.5) +
labs(subtitle = 'NARW') +
geom_sf(data = narw_sma, fill = 'red', alpha = 0.5) +
geom_sf(data = filter(data_subset, species == 'Right whale'),
aes(fill = detected, alpha = detected, geometry = geom),
show.legend = FALSE, color = NA) +
geom_sf(data = coastline) +
scale_fill_manual(values = c("black", "red")) +
scale_alpha_manual(values = c(0.1, 1))+
coord_sf(xlim = c(-1e5, 3e5),
ylim = c(-5e5, 6e5),
crs = projection) +
theme_minimal()
fin_plot <- ggplot() +
geom_sf(data = filter(data_subset, species == 'Fin whale'),
aes(fill = detected, alpha = detected, geometry = geom),
show.legend = FALSE, color = "black") +
geom_sf(data = coastline) +
geom_sf(data = st_centroid(filter(data_subset, species == 'Fin whale'))) +
scale_fill_manual(values = c("black", "red")) +
scale_alpha_manual(values = c(0.1, 1))+
coord_sf(xlim = c(-1e5, 3e5),
ylim = c(-5e5, 6e5),
crs = projection) +
labs(subtitle = 'Fin whale') +
theme_minimal()
sei_plot <- base +
geom_sf(data = filter(data_subset, species == 'Sei whale'),
aes(fill = detected, alpha = detected, geometry = geom),
show.legend = FALSE, color = NA) +
scale_fill_manual(values = c("black", "red")) +
scale_alpha_manual(values = c(0.1, 1)) +
coord_sf(xlim = c(-1e5, 3e5),
ylim = c(-5e5, 6e5),
crs = projection) +
labs(subtitle = 'Sei whale') +
theme_minimal()
humpback_plot <- base +
geom_sf(data = filter(data_subset, species == 'Humpback whale'),
aes(fill = detected, alpha = detected, geometry = geom),
show.legend = FALSE, color = NA) +
scale_fill_manual(values = c("black", "red")) +
scale_alpha_manual(values = c(0.1, 1)) +
coord_sf(xlim = c(-1e5, 3e5),
ylim = c(-5e5, 6e5),
crs = projection) +
labs(subtitle = 'Humpback') +
theme_minimal()
narw_plot + fin_plot + sei_plot + humpback_plot +
plot_layout(nrow = 1)
}
flyway_plot(yesterday)
flyway_plot(day_before)
data_subset <- rtwb |>
filter(Date %in% seq.Date(Sys.Date() - 7, Sys.Date() - 1, by = "day")) |>
group_by(species, station) |>
summarize(pct = n() / 7)
flyway_plot_summary <- function(data_subset){
narw_plot <-
ggplot() +
geom_sf(data = narw_usa, fill = 'pink', color = NA, alpha = 0.5) +
labs(subtitle = 'NARW') +
geom_sf(data = narw_sma, fill = 'red', alpha = 0.5) +
geom_sf(data = filter(one_week, species == 'Right whale'),
aes(fill = pct, geometry = geom),
color = NA) +
scale_fill_viridis_c(option = "H") +
geom_sf(data = coastline) +
coord_sf(xlim = c(-1e5, 3e5),
ylim = c(-5e5, 6e5),
crs = projection) +
theme_minimal()
fin_plot <- ggplot() +
geom_sf(data = filter(data_subset, species == 'Fin whale'),
aes(fill = pct, geometry = geom),
color = "black") +
geom_sf(data = coastline) +
geom_sf(data = st_centroid(filter(data_subset, species == 'Fin whale'))) +
coord_sf(xlim = c(-1e5, 3e5),
ylim = c(-5e5, 6e5),
crs = projection) +
labs(subtitle = 'Fin whale') +
theme_minimal()
sei_plot <- base +
geom_sf(data = filter(data_subset, species == 'Sei whale'),
aes(fill = pct, geometry = geom), color = NA) +
coord_sf(xlim = c(-1e5, 3e5),
ylim = c(-5e5, 6e5),
crs = projection) +
labs(subtitle = 'Sei whale') +
theme_minimal()
humpback_plot <- base +b
geom_sf(data = filter(data_subset, species == 'Humpback whale'),
aes(fill = pct, geometry = geom),
color = NA) +
coord_sf(xlim = c(-1e5, 3e5),
ylim = c(-5e5, 6e5),
crs = projection) +
labs(subtitle = 'Humpback') +
theme_minimal()
narw_plot + fin_plot + sei_plot + humpback_plot +
plot_layout(nrow = 1, guides = 'collect') +scale_color_viridis_c(option = "B")
}
flyway_plot_summary(one_week)