-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchange_analysis_examples.Rmd
377 lines (290 loc) · 17 KB
/
change_analysis_examples.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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
---
title: "CUU Habitat Change - Analysis of change flag"
date: "12/08/2020"
author: 'JNCC'
licence: 'MIT licence'
output:
html_document:
df_print: paged
css: style.css
includes:
before_body: header.html
pdf_document: default
always_allow_html: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(sf)
library(ggplot2)
library(dplyr)
library(tidyr)
library(plotly)
library(DT)
```
## Example site - Insh Marshes
```{r, include=F,eval=F}
#load in the data and flag change
#plot differences
dirpath <- 'Scotland/'
hab_stat <- read.csv(paste0(dirpath,"Statistics/InshMarshes_EUNIS/Seasonal_statistics/InshMarshes_NDVI_seasonal_changestats.txt"))
poly_stat <- read.csv(paste0(dirpath,"Statistics/InshMarshes_EUNIS/Seasonal_statistics/InshMarshes_NDVI_seasonal_stats.txt"))
polygons <- sf::st_read(paste0(dirpath,"Data/Habitat_maps/Scotland/ELCS_Scottish_site_1.shp")) %>% sf::st_drop_geometry()
#join the data
poly_hab <- poly_stat %>% dplyr::left_join(polygons[,c('Id','EUNIS_DESC')], by=c('ID'='Id'))
hab_stat <- hab_stat %>% dplyr::select(-X) %>% dplyr::rename(EUNIS_DESC = 'get.habclass.')
poly_all <- poly_hab %>% dplyr::left_join(hab_stat,by=c('seasonyear','EUNIS_DESC'))
#order factor levels - again
##get unique years
all_lev <- stringr::str_split(levels(poly_all$seasonyear),"_",simplify=T) %>% data.frame() %>% dplyr::mutate_if(is.character,as.factor)
#order by season and year
all_lev$X1 <- factor(all_lev$X1, levels = c('Spring','Summer','Autumn','Winter'),ordered=T)
lev_sort <- all_lev %>% arrange(X1) %>% dplyr::arrange(X2) %>% tidyr::unite(yearseason, sep="_")
##sort levels in data
poly_all$seasonyear <- factor(poly_all$seasonyear, levels = lev_sort$yearseason)
# flag those above or below 2SD as change
##above 2 SD
polygon_change <- poly_all %>% dplyr::mutate(hab_mean_poschange = hab_mean+(2*hab_meansd),
hab_mean_negchange = hab_mean-(2*hab_meansd),
hab_median_poschange = hab_median+(2*hab_mediansd),
hab_median_negchange = hab_median-(2*hab_mediansd),
hab_min_poschange = hab_min+(2*hab_minsd),
hab_min_negchange = hab_min-(2*hab_minsd),
hab_max_poschange = hab_max+(2*hab_maxsd),
hab_max_negchange = hab_max-(2*hab_maxsd),
hab_Q1_poschange = hab_Q1+(2*hab_Q1sd),
hab_Q1_negchange = hab_Q1-(2*hab_Q1sd),
hab_Q3_poschange = hab_Q1+(2*hab_Q3sd),
hab_Q3_negchange = hab_Q1-(2*hab_Q3sd)) %>%
dplyr::mutate(meanchange = ifelse(mean <hab_mean_negchange,1,0 ),
medianchange = ifelse(median <hab_median_negchange,1,0 ),
minchange = ifelse(min <hab_min_negchange,1,0 ),
maxchange = ifelse(max <hab_max_negchange,1,0 ),
Q1change = ifelse(Q1 <hab_Q1_negchange,1,0 ),
Q3change = ifelse(Q3 <hab_Q3_negchange,1,0 ))
```
## Numbers of polygons flagged as change
### When change is calculated by 1 standard deviations from the mean for the habitat type:
```{r, echo=F,eval=F}
##above 3 SD
polygon_change_1SD <- poly_all %>% dplyr:: mutate(hab_mean_poschange = hab_mean+hab_meansd,
hab_mean_negchange = hab_mean-hab_meansd,
hab_median_poschange = hab_median+hab_mediansd,
hab_median_negchange = hab_median-hab_mediansd,
hab_min_poschange = hab_min+hab_minsd,
hab_min_negchange = hab_min-hab_minsd,
hab_max_poschange = hab_max+hab_maxsd,
hab_max_negchange = hab_max-hab_maxsd,
hab_Q1_poschange = hab_Q1+hab_Q1sd,
hab_Q1_negchange = hab_Q1-hab_Q1sd,
hab_Q3_poschange = hab_Q1+hab_Q3sd,
hab_Q3_negchange = hab_Q1-hab_Q3sd) %>%
dplyr::mutate(meanchange = ifelse(mean <hab_mean_negchange,1,0 ),
medianchange = ifelse(median <hab_median_negchange,1,0 ),
minchange = ifelse(min <hab_min_negchange,1,0 ),
maxchange = ifelse(max <hab_max_negchange,1,0 ),
Q1change = ifelse(Q1 <hab_Q1_negchange,1,0 ),
Q3change = ifelse(Q3 <hab_Q3_negchange,1,0 ))
#flagged summary
flagged_1SD <- polygon_change_1SD %>% dplyr::group_by(index, seasonyear, EUNIS_DESC) %>% dplyr::summarise(totalpolygons=n(), meanchange=sum(meanchange),medianchange=sum(medianchange),Q1change=sum(Q1change),Q3change=sum(Q3change))
datatable(flagged_1SD)
```
### When change is calculated by 2 standard deviations from the mean for the habitat type:
```{r, echo=F,eval=F}
flagged <- polygon_change %>% dplyr::group_by(index, seasonyear, EUNIS_DESC) %>% dplyr::summarise(totalpolygons=n(),meanchange=sum(meanchange),medianchange=sum(medianchange),Q1change=sum(Q1change),Q3change=sum(Q3change))
datatable(flagged)
```
### When change is calculated by 3 standard deviations from the mean for the habitat type:
```{r, echo=F,eval=F}
##above 3 SD
polygon_change_3SD <- poly_all %>% dplyr::mutate(hab_mean_poschange = hab_mean+(3*hab_meansd),
hab_mean_negchange = hab_mean-(3*hab_meansd),
hab_median_poschange = hab_median+(3*hab_mediansd),
hab_median_negchange = hab_median-(3*hab_mediansd),
hab_min_poschange = hab_min+(3*hab_minsd),
hab_min_negchange = hab_min-(3*hab_minsd),
hab_max_poschange = hab_max+(3*hab_maxsd),
hab_max_negchange = hab_max-(3*hab_maxsd),
hab_Q1_poschange = hab_Q1+(3*hab_Q1sd),
hab_Q1_negchange = hab_Q1-(3*hab_Q1sd),
hab_Q3_poschange = hab_Q1+(3*hab_Q3sd),
hab_Q3_negchange = hab_Q1-(3*hab_Q3sd)) %>%
dplyr::mutate(meanchange = ifelse(mean <hab_mean_negchange,1,0 ),
medianchange = ifelse(median <hab_median_negchange,1,0 ),
minchange = ifelse(min <hab_min_negchange,1,0 ),
maxchange = ifelse(max <hab_max_negchange,1,0 ),
Q1change = ifelse(Q1 <hab_Q1_negchange,1,0 ),
Q3change = ifelse(Q3 <hab_Q3_negchange,1,0 ))
#flagged summary
flagged_3SD <- polygon_change_3SD %>% dplyr::group_by(index, seasonyear, EUNIS_DESC) %>% dplyr::summarise(totalpolygons=n(),meanchange=sum(meanchange),medianchange=sum(medianchange),Q1change=sum(Q1change),Q3change=sum(Q3change))
datatable(flagged_3SD)
```
## Average habitat statistics per month per season
```{r, echo=F,eval=F}
hab_stat <- polygon_change %>%
dplyr::select(index, seasonyear, EUNIS_DESC, hab_mean, hab_meansd,hab_median,hab_mediansd,hab_Q1,hab_Q1sd,hab_Q3,hab_Q3sd) %>%
unique()
#get total polygons per habitat
totalhab <- polygon_change %>%
dplyr::select(ID,seasonyear, EUNIS_DESC) %>%
dplyr::group_by(seasonyear,EUNIS_DESC) %>% dplyr::summarise(totalpolygons=n()) %>%
dplyr::left_join(hab_stat,by=c('seasonyear','EUNIS_DESC'))
datatable(totalhab)
```
## Exploring polygon changes
The plots below show seasonal polygon statistics compared to the mean seasonal habitat statistics, with ranges within 1SD (in grey) and 2SD (in red). Those listed below which are flagged as "change" are where a value for the season has fallen below the lower habitat range.
### Polygon id: 531
```{r, echo=F,eval=F}
#---------------------#
#filter to selected polygon and stat
poly_id = 531
stat <-'mean'
#---------------------#
polygondat <- polygon_change %>% dplyr::filter(ID==poly_id) %>% dplyr::arrange(seasonyear)
p <- ggplot2::ggplot(polygondat)+
ggplot2::geom_point(ggplot2::aes(x = seasonyear, y = get(stat), group=EUNIS_DESC), colour = "#1E1E1E") +
ggplot2::geom_line(ggplot2::aes(x = seasonyear, y = get(stat), group=EUNIS_DESC), colour = "#1E1E1E") +
ggplot2::geom_point(ggplot2::aes(x = seasonyear, y = get(paste0('hab_',stat)), group=EUNIS_DESC), colour = "#3F9C35") +
ggplot2::geom_line(ggplot2::aes(x = seasonyear, y = get(paste0('hab_',stat)), group=EUNIS_DESC), colour = "#3F9C35") +
ggplot2::theme_classic()+
ggplot2::labs(y = paste0(stat,"NDVI")) +
ggplot2::theme(axis.title.x = ggplot2::element_blank(),axis.text.x=ggplot2::element_text(angle=90))
#separate as geom_ribbon takes continuous data
p2 <- p +
ggplot2::geom_ribbon(aes(x = 1:length(seasonyear), ymin = get(paste0('hab_',stat))-(2*get(paste0('hab_',stat,'sd'))), ymax=get(paste0('hab_',stat))+(2*get(paste0('hab_',stat,'sd')))), fill="red",alpha=0.1) +
ggplot2::geom_ribbon(aes(x = 1:length(seasonyear), ymin = get(paste0('hab_',stat))-get(paste0('hab_',stat,'sd')), ymax=get(paste0('hab_',stat))+get(paste0('hab_',stat,'sd'))), fill="grey70",alpha=0.5)
plotly::ggplotly(p2)
```
```{r, echo=F,eval=F}
# polygons flagged as change
polygondat %>% dplyr::filter(get(paste0(stat,'change'))==1) %>% dplyr::select(ID,index,seasonyear,date)
```
### Polygon id: 14993
```{r, echo=F,eval=F}
#---------------------#
#filter to selected polygon and stat
poly_id = 14993
stat <-'mean'
#---------------------#
polygondat <- polygon_change %>% dplyr::filter(ID==poly_id) %>% dplyr::arrange(seasonyear)
p <- ggplot2::ggplot(polygondat)+
ggplot2::geom_point(ggplot2::aes(x = seasonyear, y = get(stat), group=EUNIS_DESC), colour = "#1E1E1E") +
ggplot2::geom_line(ggplot2::aes(x = seasonyear, y = get(stat), group=EUNIS_DESC), colour = "#1E1E1E") +
ggplot2::geom_point(ggplot2::aes(x = seasonyear, y = get(paste0('hab_',stat)), group=EUNIS_DESC), colour = "#3F9C35") +
ggplot2::geom_line(ggplot2::aes(x = seasonyear, y = get(paste0('hab_',stat)), group=EUNIS_DESC), colour = "#3F9C35") +
ggplot2::theme_classic()+
ggplot2::labs(y = paste0(stat,"NDVI")) +
ggplot2::theme(axis.title.x = ggplot2::element_blank(),axis.text.x=ggplot2::element_text(angle=90))
#separate as geom_ribbon takes continuous data
p2 <- p +
ggplot2::geom_ribbon(aes(x = 1:length(seasonyear), ymin = get(paste0('hab_',stat))-(2*get(paste0('hab_',stat,'sd'))), ymax=get(paste0('hab_',stat))+(2*get(paste0('hab_',stat,'sd')))), fill="red",alpha=0.1) +
ggplot2::geom_ribbon(aes(x = 1:length(seasonyear), ymin = get(paste0('hab_',stat))-get(paste0('hab_',stat,'sd')), ymax=get(paste0('hab_',stat))+get(paste0('hab_',stat,'sd'))), fill="grey70",alpha=0.5)
plotly::ggplotly(p2)
```
```{r, echo=F,eval=F}
# polygons flagged as change
polygondat %>% dplyr::filter(get(paste0(stat,'change'))==1) %>% dplyr::select(ID,index,seasonyear,date)
```
### Polygon id: 6907
```{r, echo=F,eval=F}
#---------------------#
#filter to selected polygon and stat
poly_id = 6907
stat <-'mean'
#---------------------#
polygondat <- polygon_change %>% dplyr::filter(ID==poly_id) %>% dplyr::arrange(seasonyear)
p <- ggplot2::ggplot(polygondat)+
ggplot2::geom_point(ggplot2::aes(x = seasonyear, y = get(stat), group=EUNIS_DESC), colour = "#1E1E1E") +
ggplot2::geom_line(ggplot2::aes(x = seasonyear, y = get(stat), group=EUNIS_DESC), colour = "#1E1E1E") +
ggplot2::geom_point(ggplot2::aes(x = seasonyear, y = get(paste0('hab_',stat)), group=EUNIS_DESC), colour = "#3F9C35") +
ggplot2::geom_line(ggplot2::aes(x = seasonyear, y = get(paste0('hab_',stat)), group=EUNIS_DESC), colour = "#3F9C35") +
ggplot2::theme_classic()+
ggplot2::labs(y = paste0(stat,"NDVI")) +
ggplot2::theme(axis.title.x = ggplot2::element_blank(),axis.text.x=ggplot2::element_text(angle=90))
#separate as geom_ribbon takes continuous data
p2 <- p +
ggplot2::geom_ribbon(aes(x = 1:length(seasonyear), ymin = get(paste0('hab_',stat))-(2*get(paste0('hab_',stat,'sd'))), ymax=get(paste0('hab_',stat))+(2*get(paste0('hab_',stat,'sd')))), fill="red",alpha=0.1) +
ggplot2::geom_ribbon(aes(x = 1:length(seasonyear), ymin = get(paste0('hab_',stat))-get(paste0('hab_',stat,'sd')), ymax=get(paste0('hab_',stat))+get(paste0('hab_',stat,'sd'))), fill="grey70",alpha=0.5)
plotly::ggplotly(p2)
```
```{r, echo=F,eval=F}
# polygons flagged as change
polygondat %>% dplyr::filter(get(paste0(stat,'change'))==1) %>% dplyr::select(ID,index,seasonyear,date)
```
### Polygon id: 14519
```{r, echo=F,eval=F}
#---------------------#
#filter to selected polygon and stat
poly_id = 14519
stat <-'mean'
#---------------------#
polygondat <- polygon_change %>% dplyr::filter(ID==poly_id) %>% dplyr::arrange(seasonyear)
p <- ggplot2::ggplot(polygondat)+
ggplot2::geom_point(ggplot2::aes(x = seasonyear, y = get(stat), group=EUNIS_DESC), colour = "#1E1E1E") +
ggplot2::geom_line(ggplot2::aes(x = seasonyear, y = get(stat), group=EUNIS_DESC), colour = "#1E1E1E") +
ggplot2::geom_point(ggplot2::aes(x = seasonyear, y = get(paste0('hab_',stat)), group=EUNIS_DESC), colour = "#3F9C35") +
ggplot2::geom_line(ggplot2::aes(x = seasonyear, y = get(paste0('hab_',stat)), group=EUNIS_DESC), colour = "#3F9C35") +
ggplot2::theme_classic()+
ggplot2::labs(y = paste0(stat,"NDVI")) +
ggplot2::theme(axis.title.x = ggplot2::element_blank(),axis.text.x=ggplot2::element_text(angle=90))
#separate as geom_ribbon takes continuous data
p2 <- p +
ggplot2::geom_ribbon(aes(x = 1:length(seasonyear), ymin = get(paste0('hab_',stat))-(2*get(paste0('hab_',stat,'sd'))), ymax=get(paste0('hab_',stat))+(2*get(paste0('hab_',stat,'sd')))), fill="red",alpha=0.1) +
ggplot2::geom_ribbon(aes(x = 1:length(seasonyear), ymin = get(paste0('hab_',stat))-get(paste0('hab_',stat,'sd')), ymax=get(paste0('hab_',stat))+get(paste0('hab_',stat,'sd'))), fill="grey70",alpha=0.5)
plotly::ggplotly(p2)
```
```{r, echo=F,eval=F}
# polygons flagged as change
polygondat %>% dplyr::filter(get(paste0(stat,'change'))==1) %>% dplyr::select(ID,index,seasonyear,date)
```
### Polygon id: 3344
```{r,echo=F,eval=F}
#---------------------#
#filter to selected polygon and stat
poly_id = 3344
stat <-'mean'
#---------------------#
polygondat <- polygon_change %>% dplyr::filter(ID==poly_id) %>% dplyr::arrange(seasonyear)
p <- ggplot2::ggplot(polygondat)+
ggplot2::geom_point(ggplot2::aes(x = seasonyear, y = get(stat), group=EUNIS_DESC), colour = "#1E1E1E") +
ggplot2::geom_line(ggplot2::aes(x = seasonyear, y = get(stat), group=EUNIS_DESC), colour = "#1E1E1E") +
ggplot2::geom_point(ggplot2::aes(x = seasonyear, y = get(paste0('hab_',stat)), group=EUNIS_DESC), colour = "#3F9C35") +
ggplot2::geom_line(ggplot2::aes(x = seasonyear, y = get(paste0('hab_',stat)), group=EUNIS_DESC), colour = "#3F9C35") +
ggplot2::theme_classic()+
ggplot2::labs(y = paste0(stat,"NDVI")) +
ggplot2::theme(axis.title.x = ggplot2::element_blank(),axis.text.x=ggplot2::element_text(angle=90))
#separate as geom_ribbon takes continuous data
p2 <- p +
ggplot2::geom_ribbon(aes(x = 1:length(seasonyear), ymin = get(paste0('hab_',stat))-(2*get(paste0('hab_',stat,'sd'))), ymax=get(paste0('hab_',stat))+(2*get(paste0('hab_',stat,'sd')))), fill="red",alpha=0.1) +
ggplot2::geom_ribbon(aes(x = 1:length(seasonyear), ymin = get(paste0('hab_',stat))-get(paste0('hab_',stat,'sd')), ymax=get(paste0('hab_',stat))+get(paste0('hab_',stat,'sd'))), fill="grey70",alpha=0.5)
plotly::ggplotly(p2)
```
```{r, echo=F,eval=F}
# polygons flagged as change
polygondat %>% dplyr::filter(get(paste0(stat,'change'))==1) %>% dplyr::select(ID,index,seasonyear,date)
```
### Polygon id: 3862
```{r, echo=F,eval=F}
#---------------------#
#filter to selected polygon and stat
poly_id = 3862
stat <-'mean'
#---------------------#
polygondat <- polygon_change %>% dplyr::filter(ID==poly_id) %>% dplyr::arrange(seasonyear)
p <- ggplot2::ggplot(polygondat)+
ggplot2::geom_point(ggplot2::aes(x = seasonyear, y = get(stat), group=EUNIS_DESC), colour = "#1E1E1E") +
ggplot2::geom_line(ggplot2::aes(x = seasonyear, y = get(stat), group=EUNIS_DESC), colour = "#1E1E1E") +
ggplot2::geom_point(ggplot2::aes(x = seasonyear, y = get(paste0('hab_',stat)), group=EUNIS_DESC), colour = "#3F9C35") +
ggplot2::geom_line(ggplot2::aes(x = seasonyear, y = get(paste0('hab_',stat)), group=EUNIS_DESC), colour = "#3F9C35") +
ggplot2::theme_classic()+
ggplot2::labs(y = paste0(stat,"NDVI")) +
ggplot2::theme(axis.title.x = ggplot2::element_blank(),axis.text.x=ggplot2::element_text(angle=90))
#separate as geom_ribbon takes continuous data
p2 <- p +
ggplot2::geom_ribbon(aes(x = 1:length(seasonyear), ymin = get(paste0('hab_',stat))-(2*get(paste0('hab_',stat,'sd'))), ymax=get(paste0('hab_',stat))+(2*get(paste0('hab_',stat,'sd')))), fill="red",alpha=0.1) +
ggplot2::geom_ribbon(aes(x = 1:length(seasonyear), ymin = get(paste0('hab_',stat))-get(paste0('hab_',stat,'sd')), ymax=get(paste0('hab_',stat))+get(paste0('hab_',stat,'sd'))), fill="grey70",alpha=0.5)
plotly::ggplotly(p2)
```
```{r, echo=F,eval=F}
# polygons flagged as change
polygondat %>% dplyr::filter(get(paste0(stat,'change'))==1) %>% dplyr::select(ID,index,seasonyear,date)
```