forked from BjnNowak/TidyTuesday
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsc_anim_flood.R
314 lines (284 loc) · 7.9 KB
/
sc_anim_flood.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# Clear space
rm(list=ls())
gc()
library(tidyverse)
library(camcorder)
library(ggtext)
library(showtext)
library(maps)
library(sf)
library(ggiraph)
library(cowplot)
library(magick)
library(raster)
library(rasterVis)
library(rgdal)
library(grid)
library(scales)
library(viridis) # better colors for everyone
library(ggthemes) # theme_map()
font_add_google("Permanent Marker","marker")
font_add_google("Open Sans","open")
showtext_auto()
#com <- readOGR(dsn='Data', layer="Com63")
com<-read_sf(
dsn = "Data",
layer = "Com63"
)%>%
filter(INSEE_DEP==63)
com_diss<-com%>%
group_by(INSEE_DEP)%>%
summarise()%>%
ungroup()
water<-read_sf(
dsn = "Data",
layer = "COURS_D_EAU"
)
# Intersection
water_clip <- st_intersection(water, com_diss)
# Filter
water_sel <- water_clip%>%
filter(TOPONYME=="l'Allier")
# Buffer
water_buffer <- st_buffer(water_sel, 5000)
# Intersection (for buffer)
water_buff_clip <- st_intersection(water_buffer, com_diss)
# Difference
com_diff<-st_difference(com_diss,water_buff_clip)
# Get extent
x_min <- extent(com)[1]
x_max <- extent(com)[2]
y_min <- extent(com)[3]
y_max <- extent(com)[4]
# Plot size
gg_record(
dir = file.path(tempdir(),"recording"),
device = "png",
width = 10,
height = 10,
units = "cm",
dpi = 300
)
# Set colors
col_fill <- '#e9d8a6'
col_col <- '#001219'
col_water <- "#94d2bd"
# Plots
p1<-ggplot()+
geom_sf(
com,mapping=aes(geometry=geometry),
fill=col_fill,color=col_col,size=0.4
)+
labs(
title="1. Basemap",
subtitle='Puy-de-Dôme department (France)'
)+
scale_x_continuous(limits=c(x_min,x_max))+
scale_y_continuous(limits=c(y_min,y_max))+
coord_sf() +
theme_map() +
theme(
plot.background = element_rect(fill="white",color=NA),
plot.title = element_text(hjust = 0.5,size=40,family='marker',lineheight=0.45,color=col_col),
plot.subtitle = element_markdown(hjust = 0.5,size=30,family='open',lineheight=0.45,color=col_col)
)
p1
p2<-ggplot()+
geom_sf(
com_diss,mapping=aes(geometry=geometry),
fill=col_fill,color=col_col,size=0.4
)+
labs(
title="2. Dissolve",
subtitle="Merge all municipalities"
)+
coord_sf() +
theme_map() +
theme(
plot.background = element_rect(fill="white",color=NA),
plot.title = element_text(hjust = 0.5,size=40,family='marker',lineheight=0.45,color=col_col),
plot.subtitle = element_markdown(hjust = 0.5,size=30,family='open',lineheight=0.45,color=col_col)
)
p2
p3<-ggplot()+
geom_sf(
com_diss,mapping=aes(geometry=geometry),
fill=col_fill,color=col_col,size=0.4
)+
geom_sf(
water,mapping=aes(geometry=geometry),
color='#0a9396',size=0.4
)+
labs(
title="3. Add layer",
subtitle="French rivers layer"
)+
scale_x_continuous(limits=c(x_min,x_max))+
scale_y_continuous(limits=c(y_min,y_max))+
coord_sf() +
theme_map() +
theme(
plot.background = element_rect(fill="white",color=NA),
plot.title = element_text(hjust = 0.5,size=40,family='marker',lineheight=0.45,color=col_col),
plot.subtitle = element_markdown(hjust = 0.5,size=30,family='open',lineheight=0.45,color=col_col)
)
p3
p4<-ggplot()+
geom_sf(
com_diss,mapping=aes(geometry=geometry),
fill=col_fill,color=col_col,size=0.4
)+
geom_sf(
water_clip,mapping=aes(geometry=geometry),
color='#0a9396',size=0.3
)+
labs(
title="4. Intersection",
subtitle="Crop rivers by department boundaries"
)+
scale_x_continuous(limits=c(x_min,x_max))+
scale_y_continuous(limits=c(y_min,y_max))+
coord_sf() +
theme_map() +
theme(
plot.background = element_rect(fill="white",color=NA),
plot.title = element_text(hjust = 0.5,size=40,family='marker',lineheight=0.45,color=col_col),
plot.subtitle = element_markdown(hjust = 0.5,size=30,family='open',lineheight=0.45,color=col_col)
)
p4
p5<-ggplot()+
geom_sf(
com_diss,mapping=aes(geometry=geometry),
fill=col_fill,color=col_col,size=0.4
)+
geom_sf(
water_sel,mapping=aes(geometry=geometry),
color='#0a9396',size=0.4
)+
labs(
title="5. Filter",
subtitle="Select main river"
)+
scale_x_continuous(limits=c(x_min,x_max))+
scale_y_continuous(limits=c(y_min,y_max))+
coord_sf() +
theme_map() +
theme(
plot.background = element_rect(fill="white",color=NA),
plot.title = element_text(hjust = 0.5,size=40,family='marker',lineheight=0.45,color=col_col),
plot.subtitle = element_markdown(hjust = 0.5,size=30,family='open',lineheight=0.45,color=col_col)
)
p5
p6<-ggplot()+
geom_sf(
com_diss,mapping=aes(geometry=geometry),
fill=col_fill,color=col_col,size=0.4
)+
geom_sf(
water_buffer,mapping=aes(geometry=geometry),
fill='#94d2bd',color='#005f73',size=0.4
)+
geom_sf(
water_sel,mapping=aes(geometry=geometry),
color='#0a9396',size=0.4
)+
labs(
title="6. Buffer",
subtitle="Fixed distance buffer around the river"
)+
scale_x_continuous(limits=c(x_min,x_max))+
scale_y_continuous(limits=c(y_min,y_max))+
coord_sf() +
theme_map() +
theme(
plot.background = element_rect(fill="white",color=NA),
plot.title = element_text(hjust = 0.5,size=40,family='marker',lineheight=0.45,color=col_col),
plot.subtitle = element_markdown(hjust = 0.5,size=30,family='open',lineheight=0.45,color=col_col)
)
p6
p7<-ggplot()+
geom_sf(
com_diss,mapping=aes(geometry=geometry),
fill=col_fill,color=col_col,size=0.4
)+
geom_sf(
water_buff_clip,mapping=aes(geometry=geometry),
fill='#94d2bd',color='#005f73',size=0.4
)+
labs(
title="7. Intersection",
subtitle="Crop buffer by department boundaries"
)+
scale_x_continuous(limits=c(x_min,x_max))+
scale_y_continuous(limits=c(y_min,y_max))+
coord_sf() +
theme_map() +
theme(
plot.background = element_rect(fill="white",color=NA),
plot.title = element_text(hjust = 0.5,size=40,family='marker',lineheight=0.45,color=col_col),
plot.subtitle = element_markdown(hjust = 0.5,size=30,family='open',lineheight=0.45,color=col_col)
)
p7
p8a<-ggplot()+
#geom_sf(
# water_buff_clip,mapping=aes(geometry=geometry),
# color='#3DA5D9',size=0.4
#)+
geom_sf(
com_diff,mapping=aes(geometry=geometry),
fill=col_fill,color=col_col,size=0.4
)+
labs(
title="8. Difference",
subtitle="No risk of flooding"
)+
scale_x_continuous(limits=c(x_min,x_max))+
scale_y_continuous(limits=c(y_min,y_max))+
coord_sf() +
theme_map() +
theme(
plot.background = element_rect(fill="white",color=NA),
plot.title = element_text(hjust = 0.5,size=40,family='marker',lineheight=0.45,color=col_col),
plot.subtitle = element_markdown(hjust = 0.5,size=30,family='open',lineheight=0.45,color=col_col)
)
p8a
p8b<-ggplot()+
geom_sf(
water_buff_clip,mapping=aes(geometry=geometry),
fill='#94d2bd',color='#005f73',size=0.4
)+
#geom_sf(
# com_diff,mapping=aes(geometry=geometry),
# fill=col_fill,color=col_col,size=0.4
#)+
labs(
title="8. Difference",
subtitle="Risk of flooding"
)+
scale_x_continuous(limits=c(x_min,x_max))+
scale_y_continuous(limits=c(y_min,y_max))+
coord_sf() +
theme_map() +
theme(
plot.background = element_rect(fill="white",color=NA),
plot.title = element_text(hjust = 0.5,size=40,family='marker',lineheight=0.45,color=col_col),
plot.subtitle = element_markdown(hjust = 0.5,size=30,family='open',lineheight=0.45,color=col_col)
)
p8b
# Animation
#p1
#p2
#p3
#p4
#p5
#p6
#p7
#p8a
#p8b
imgs <- list.files(file.path(tempdir(),"recording"), full.names = TRUE)
img_list <- lapply(imgs, image_read)
## join the images together
img_joined <- image_join(img_list)
## animate at 2 frames per second
img_animated <- image_animate(img_joined, fps = 0.5)
img_animated