forked from NINAnor/ecosystemCondition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedianSummerTemperature.Rmd
718 lines (524 loc) · 23.1 KB
/
medianSummerTemperature.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
# Median Summer Temperature {#median-summer-temp}
<br />
*Author and date:*
Anders L. Kolstad
March 2023
<br />
<!-- Load all you dependencies here -->
```{r setup, include=FALSE}
library(knitr)
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(stars)
library(sf)
library(tmap)
library(lubridate)
library(ggpubr)
library(microbenchmark)
library(tictoc)
library(ncmeta)
library(parallel)
library(exactextractr)
library(data.table)
library(terra)
dir <- substr(getwd(), 1,2)
pData <- ifelse(dir == "C:",
"P:/41201785_okologisk_tilstand_2022_2023/data/",
"/data/P-Prosjekter2/41201785_okologisk_tilstand_2022_2023/data/")
```
```{r, echo=F}
Ecosystem <- "All"
Egenskap <- "Abiotiske egenskaper"
ECT <- "Physical state characteristics"
Contact <- "Anders L. Kolstad"
```
```{r, echo=F}
metaData <- data.frame(Ecosystem,
"Økologisk egenskap" = Egenskap,
"ECT class" = ECT)
knitr::kable(metaData)
```
<br /> <br />
<hr />
## Introduction
This chapters describes the workflow for an indicator describing the median summer temperature. For a more comprehensive documentation on the development of the workflow itself, see [here](#climate-indicators-explained). The data comes from interpolated climate surfaces from [SeNorge](https://senorge.no/) which contain one 1x1km raster for each day since 1957 to present. The reference levels are extracted from the time period 1961-1990 (a temporal reference condition).
**Workflow**
1. Collate variable data series, ending up with one raster per year after aggregating across days within a year
2. Calculate spatially explicit reference values by aggregating across the years 1961-1990. The upper reference value is equal to the median value of this period, and the lower reference values (two-sided) is equal to 5 SD units. The indicator value is the mean over a five year period.
3. Calculate indicator values through spatially explicit rescaling based on the reference values
4. Mask by ecosystem type (*This step is not yet done as we do not have ready available ecosystem maps*)
5. Aggregate in space (to accounting areas) and take the mean over a five year period to get final indicator values
6. Make trend figure and spatially aggregated maps
## About the underlying data
The data is in a raster format and extends back to 1957 in the form of multiple interpolated climate variables. The spatial resolution is 1 x 1 km.
### Representativity in time and space
The data includes the last normal period (1961-1990) which defines the reference condition for climate variables. Therefore the temporal resolution is very good. Also considering the daily resolution of the data.
Spatially, a 1x1 km resolution is sufficient for most climate variables, esp. in homogeneous terrain, but this needs to be evaluation for each variable and scenario specifically.
### Original units
Varied. Specified below for each parameter.
### Temporal coverage
1957 - present
### Additional comments about the data set
The data format has recently changed from .BIL to .nc (netcdf) and now a single file contains all the rasters for one year (365 days), and sometimes for multiple variables also.
## Ecosystem characteristic
### Norwegian standard
These variables typically will fall under the *abiotiske egenskaper* class.
### SEEA EA
In SEEA EA, these variables will typically fall under A1 - Physical state characteristics.
## Collinearities with other indicators
Climate variables are most likely to be correlated with each other (e.g. temperature and snow). Also, some climate variables are better classed as pressure indicators, and these might have a causal association with several condition indicators.
## Reference condition and values
### Reference condition
The reference condition for climate variables is defined as the normal period 1961-1990.
### Reference values, thresholds for defining *good ecological condition*, minimum and/or maximum values
- Un-scaled indicator value = median value over 5 year periods (5 years being a pragmatic choice. It is long enough to smooth out a lot of inter-annual variation, and it's long enough to enable estimating errors)
- Upper reference level (best possible condition) = median value from the reference period
- Thresholds for good ecosystem condition = 2 standard deviation units away from the upper reference level for the climate variable during the reference period.
- Lower reference values (two-directional) = 5 standard deviation units for the climate variable during the reference period (implies linear scaling).
The choice to use 2 SD units as the threshold values is a subjective choice in many ways, and not founded in any empirical or known relationship between the indicators and ecosystem integrity. The value comes from the common practice of calling something _extreme weather_ when it is outside 2 SD units of the current running average. So, if the indicator value today is <0.6 it implies that the mean for that variable over the last year would have been referred to as _extreme_ if it had occurred between 1961 and 1990. This is I think a conservative threshold, since one would/could call it _extreme_ if only one year is outside the 2SD range, and having the mean of 5 years being outside this range is _really_ extreme.
## Uncertainties
For the indicator map (1 x 1 km raster) there is no uncertainty associated with the indicator values. For aggregated indicator values (e.g. for regions), the uncertainty in the indicator value is calculated from the spatial variation in the indicator values via bootstrapping. This might, however, be changed later to the temporal variation between the five years of each period.
## References
<https://senorge.no/>
*rr and tm are being download from:* <https://thredds.met.no/thredds/catalog/senorge/seNorge_2018/Archive/catalog.html>
### Additional resources
[Stars package](https://r-spatial.github.io/stars/)
[R as a GIS for economists](https://tmieno2.github.io/R-as-GIS-for-Economists/stars-basics.html) chapter 7
<hl/>
## Analyses
### Data set
The data is downloaded to a local NINA server, and updated regularly.
```{r}
path <- ifelse(dir == "C:",
"R:/GeoSpatialData/Meteorology/Norway_SeNorge2018_v22.09/Original",
"/data/R/GeoSpatialData/Meteorology/Norway_SeNorge2018_v22.09/Original")
```
This folder contains folder for the different parameters
```{r}
(files <- list.files(path))
```
We are interested in _tm_, which is temperatur in celcius. For some reason the same variable is called _tg_ in the data itself.
#### Regions
Importing a shape file with the regional delineation.
```{r}
reg <- sf::st_read("data/regions.shp", options = "ENCODING=UTF8")
#st_crs(reg)
```
Outline of Norway
```{r, message=FALSE}
nor <- sf::st_read("data/outlineOfNorway_EPSG25833.shp")
```
Remove marine areas from regions
```{r, warning=F}
reg <- st_intersection(reg, nor)
```
```{r, fig.cap="Five accounting areas (regions) in Norway."}
tm_shape(reg) +
tm_polygons(col="region")
```
#### Ecosystem map
Coming soon ....
The climate indicators need to be masked with ecosystem type maps. This step is part of this chapter.
### Conceptual workflow
The general, the conceptual workflow is like this:
1. Collate variable data series
- Import .nc files (loop though year 1961-1990) and subset to the correct attribute
- Filter data by dates (optional) (`dplyr::filter`). *This means reading all 365 rasters into memory, and it is much quicker to filter out the correct rasters in the importing step above (see examples later in this chapter)*
- Aggregate across time within a year (`stars::st_apply`). *This is the most time consuming operation in the workflow.*
- Join all data into one data cube (`stars:c`)
2. Calculate reference values
- Aggregate (`st_apply)` across reference years (`dplyr::filter`) to get median and sd values
- Join with existing data cube (`stars:c`)
3. Calculate indicator values
- Normalize climate variable at the individual grid cell level using the three reference values (`mutate(across()))`
4. Mask by ecosystem type (*This could also be done in step one, but it doesn't speed things up to set some cells to NA*)
5. Aggregate in space (to accounting areas) (*zonal statistics*)
- Aggregate across 5 year time steps to smooth out random inter-annual variation and leave climate signal
- Intersect with accounting area polygons `exactextrar::exact_extract` and get mean/median and (spatial) sd. (*Alternatively, get temporal sd at the grid cell level in the step above.*)
6. Make trend figure and spatially aggregated maps
### Step 1 - temporal aggregation within a year
```{r, warning=F, eval=F}
path <- path <- ifelse(dir == "C:",
"R:/",
"/data/R/")
path2 <- paste0(path, "GeoSpatialData/Meteorology/Norway_SeNorge2018_v22.09/Original/rr_tm/")
myFiles <- list.files(path2, pattern=".nc$",full.names = T)
# The last file (the last year) is incomplete and don't include all julian dates that we select below, so I will not include it:
myFiles <- myFiles[-length(myFiles)]
real_temp_summer <- NULL
# set up parallel cluster using 10 cores
cl <- makeCluster(10L)
# Get julian days after defining months
temp <- stars::read_ncdf(paste(myFiles[1]), var="tg")
start_month_num <- 6
end_month_num <- 8
julian_start <- yday(st_get_dimension_values(temp, "time")[1] %m+%
months(+start_month_num))
julian_end <- yday(st_get_dimension_values(temp, "time")[1] %m+%
months(+end_month_num))
step <- julian_end-julian_start
for(i in 1:length(myFiles)){
tic("init")
temp <- stars::read_ncdf(paste(myFiles[i]), var="tg", proxy=F,
ncsub = cbind(start = c(1, 1, julian_start),
count = c(NA, NA, step)))
year_temp <- year(st_get_dimension_values(temp, "time")[1])
print(year_temp)
lookup <- setNames("mean", paste0("v_", year_temp))
# Perhaps leave out the v_ to get a numeric vector instead,
# which is easier to subset
st_crs(temp) <- 25833
toc()
tic("filter and st_apply")
temp <- temp %>%
#filter(time %within% myInterval) %>%
st_apply(1:2, mean, CLUSTER = cl) %>%
rename(all_of(lookup))
toc()
tic("c()")
real_temp_summer <- c(temp, real_temp_summer)
#rm(temp)
toc()
}
tic("Merge")
real_temp_summer <- real_temp_summer %>%
merge(name = "Year") %>%
setNames("climate_variable")
toc()
stopCluster(cl)
```
This takes about 20 sec per file/year, or 22 min on total. That is not too bad. About 6000 rasters are read into memory. Here's a test for the effect of splitting over more cores.
```{r, eval=F}
write_stars(real_temp_summer, "/data/P-Prosjekter2/41201785_okologisk_tilstand_2022_2023/data/climate_indicators/aggregated_climate_time_series/real_temp_summer.tiff")
```
```{r, include=F}
real_temp_summer <- read_stars(paste0(pData, "climate_indicators/aggregated_climate_time_series/real_temp_summer.tiff"),
proxy=F)
```
Note that GTiff automatically renames the third dimension *band* and also renames the attribute. I can rename them.
```{r}
summer_median_temp <- real_temp_summer %>%
st_set_dimensions(names = c("x", "y", "v_YEAR")) %>%
setNames("temperature")
```
```{r, fig.cap="Showing three random slices of the year dimension."}
ggplot()+
geom_stars(data = summer_median_temp[,,,c(1,11,66)], downsample = c(10, 10, 0)) +
coord_equal() +
theme_void() +
scale_fill_viridis_c(option = "D") +
scale_x_discrete(expand = c(0, 0)) +
scale_y_discrete(expand = c(0, 0))+
facet_wrap(~v_YEAR)
```
### Step 2 - calculate reference values
We need to first to define a reference period and then to subset our data `summer_median_temp`.
First we need to rename our dimension values and turn them back into dates.
```{r}
new_dims <- as.Date(paste0(
substr(st_get_dimension_values(summer_median_temp, "v_YEAR"), 3, 6), "-01-01"))
summer_median_temp_ref <- summer_median_temp %>%
st_set_dimensions("v_YEAR", values = new_dims)
```
Then I can filter to leave only the reference period.
```{r}
summer_median_temp_ref <- summer_median_temp_ref %>%
filter(v_YEAR %within% interval("1961-01-01", "1990-12-31"))
st_get_dimension_values(summer_median_temp_ref, "v_YEAR")
```
And then we calculate the median and sd like above
```{r}
median_sd <- function(x) { c(median = median(x), sd = sd(x))}
```
```{r, eval=FALSE}
system.time({
cl <- makeCluster(10L)
summer_median_temp_ref <- summer_median_temp_ref %>%
st_apply(c("x", "y"),
FUN = median_sd,
CLUSTER = cl)
stopCluster(cl)
})
```
| user | system | elapsed |
|-------|--------|---------|
| 9.624 | 6.069 | 20.903 |
```{r, eval=F}
write_stars(summer_median_temp_ref, "/data/P-Prosjekter2/41201785_okologisk_tilstand_2022_2023/data/climate_indicators/aggregated_climate_time_series/summer_median_temp_ref.tiff")
```
```{r, eval=T, include=F}
summer_median_temp_ref <- read_stars(paste0(pData, "climate_indicators/aggregated_climate_time_series/summer_median_temp_ref.tiff"),
proxy=F)
```
Pivot and turn dimension into attributes, and rename attributes:
```{r, eval=T}
summer_median_temp_ref_long <- summer_median_temp_ref %>%
split("band") %>%
setNames(c("reference_upper", "sd"))
```
```{r, fig.cap="Showing the upper reference levels and the standard deviation from actual data of median summer temperatures."}
tmap_arrange(
tm_shape(st_downsample(summer_median_temp_ref_long, 10))+
tm_raster("reference_upper")
,
tm_shape(st_downsample(summer_median_temp_ref_long, 10))+
tm_raster("sd",
palette = "-viridis")
)
```
I need to combine the variables and the ref values in one data cube
```{r}
y_var <- summer_median_temp %>%
split("v_YEAR") %>%
c(summer_median_temp_ref_long)
```
### Step 3 - normalise variable
```{r}
# select the columns to normalise
cols <- names(y_var)[!names(y_var) %in% c("reference_upper", "sd") ]
cols_new <- cols
names(cols_new) <- gsub("v_", "i_", cols)
```
```{r, eval=F}
# The break point scaling is actually not needed here, since
# having the lower ref value to be 5 sd implies that the threshold is
# 2 sd in a linear scaling.
system.time(
y_var_norm <- y_var %>%
mutate(reference_low = reference_upper - 5*sd ) %>%
mutate(reference_low2 = reference_upper + 5*sd ) %>%
mutate(threshold_low = reference_upper -2*sd ) %>%
mutate(threshold_high = reference_upper +2*sd ) %>%
mutate(across(all_of(cols), ~
if_else(.x < reference_upper,
if_else(.x < threshold_low,
(.x - reference_low) / (threshold_low - reference_low),
(.x - threshold_low) / (reference_upper - threshold_low),
),
if_else(.x > threshold_high,
(reference_low2 - .x) / (reference_low2 - threshold_high),
(threshold_high - .x) / (threshold_high - reference_upper),
)
))) %>%
mutate(across(all_of(cols), ~ if_else(.x > 1, 1, .x))) %>%
mutate(across(all_of(cols), ~ if_else(.x < 0, 0, .x))) %>%
rename(all_of(cols_new)) %>%
c(select(y_var, all_of(cols)))
)
```
| user | system | elapsed |
|--------|--------|---------|
| 14.803 | 2.717 | 17.512 |
```{r, eval=F}
gc()
saveRDS(y_var_norm, "/data/P-Prosjekter2/41201785_okologisk_tilstand_2022_2023/data/climate_indicators/aggregated_climate_time_series/summer_median_temp_normalised.RData")
# Tiff dont allow for multiple attributes:
#write_stars(y_var_norm, "/data/P-Prosjekter2/41201785_okologisk_tilstand_2022_2023/data/climate_indicators/aggregated_climate_time_series/summer_median_temp_normalised.tiff")
```
```{r, include=FALSE}
y_var_norm <- readRDS(paste0(pData, "climate_indicators/aggregated_climate_time_series/temp_median_summer_normalised.RData"))
```
```{r, fig.cap="Example showing median summer tempretur in 1970, the upper and lwoer reference temperture, i.e. median and 5 SD units of the temperature between 1961-1990, and finally, the scaled indicator values."}
lims <- c(-5, 22)
ggarrange(
ggplot() +
geom_stars(data = st_downsample(y_var_norm["v_1970"],10)) +
coord_equal() +
theme_void() +
scale_fill_viridis_c(option = "D",
limits = lims) +
scale_x_discrete(expand = c(0, 0)) +
scale_y_discrete(expand = c(0, 0))
,
ggplot() +
geom_stars(data = st_downsample(y_var_norm["reference_upper"], 10)) +
coord_equal() +
theme_void() +
scale_fill_viridis_c(option = "D",
limits = lims) +
scale_x_discrete(expand = c(0, 0)) +
scale_y_discrete(expand = c(0, 0))
,
ggplot() +
geom_stars(data = st_downsample(y_var_norm["reference_low"], 10)) +
coord_equal() +
theme_void() +
scale_fill_viridis_c(option = "D",
limits = lims) +
scale_x_discrete(expand = c(0, 0)) +
scale_y_discrete(expand = c(0, 0))
,
ggplot() +
geom_stars(data = st_downsample(y_var_norm["reference_low2"], 10)) +
coord_equal() +
theme_void() +
scale_fill_viridis_c(option = "D",
limits = lims) +
scale_x_discrete(expand = c(0, 0)) +
scale_y_discrete(expand = c(0, 0))
,
ggplot() +
geom_stars(data = st_downsample(y_var_norm["i_1970"],10)) +
coord_equal() +
theme_void() +
scale_fill_viridis_c(option = "A",
limits = c(0, 1)) +
scale_x_discrete(expand = c(0, 0)) +
scale_y_discrete(expand = c(0, 0))
, ncol=2, nrow=3, align = "hv"
)
```
The *real* indicator values should be means over 5 year periods. Calculating a running mean for all time steps is too time consuming. Therefore, the scaled, regional indicator values will be calculated for distinct time steps. The time series can perhaps still be presented with yearly resolution.
### Step 4 - Mask with ecosystem delineation map
This step we simply ignore for now. It should be relatively easy to do, when we have the maps.
### Step 5 - Make figures
To plot time series I first need to do a spatial aggregation.
```{r, eval=F}
system.time(
regional_means <- rast(y_var_norm) %>%
exact_extract(reg, fun = 'mean', append_cols = "region", progress=T) %>%
setNames(c("region", names(y_var_norm)))
)
```
user; system; elapsed: 7.603; 3.071; 10.697
I could also get the sd like this, if I wanted to base the indicator uncertainty on a measure of spatial variation:
```{r, eval=F}
system.time(
regional_sd <- rast(y_var_norm) %>%
exact_extract(reg, fun = 'stdev', append_cols = "region", progress=T) %>%
setNames(c("region", names(y_var_norm)))
)
```
user; system; elapsed: 7.420; 2.921; 10.355
```{r, eval=F}
saveRDS(regional_means, "temp/regional_means.rds")
saveRDS(regional_sd, "temp/regional_sd.rds")
```
```{r, include=F}
regional_means <- readRDS("temp/regional_means.rds")
#regional_sd <- readRDS("temp/regional_sd.rds")
```
Reshape and plot
```{r, warning=F}
div <- c("reference_upper",
"reference_low",
"reference_low2",
"threshold_low",
"threshold_high",
"sd")
temp <- regional_means %>%
as.data.frame() %>%
select(region, div)
regional_means_long <- regional_means %>%
as.data.frame() %>%
select(!all_of(div)) %>%
pivot_longer(!region) %>%
separate(name, into=c("type", "year")) %>%
pivot_wider(#id_cols = region,
names_from = type) %>%
left_join(temp, by=join_by(region)) %>%
mutate(diff = v-reference_upper) %>%
mutate(threshold_low_centered = threshold_low-reference_upper) %>%
mutate(threshold_high_centered = threshold_high-reference_upper)
#Adding the spatial sd
#(
#regional_means_long <- regional_sd %>%
# select(!all_of(div)) %>%
# pivot_longer(!region) %>%
# separate(name, into=c("type", "year")) %>%
# pivot_wider(names_from = type) %>%
# rename(i_sd = i,
# v_sd = v) %>%
# left_join(regional_means_long, by=join_by(region, year))
#)
```
```{r summer-temp-time-series, fig.cap="Times series for median summer temperature centered on the median value during the reference period. The reference period is indicated with a thick horizontal line. Dottet horisontal lines are 2 sd units for the reference period."}
regOrder = c("Østlandet","Sørlandet","Vestlandet","Midt-Norge","Nord-Norge")
regional_means_long %>%
mutate(col = if_else(diff>0, "1", "2")) %>%
ggplot(aes(x = as.numeric(year),
y = diff, fill = col))+
geom_bar(stat="identity")+
geom_hline(aes(yintercept = threshold_low_centered),
linetype=2)+
geom_hline(aes(yintercept = threshold_high_centered),
linetype=2)+
geom_segment(x = 1961, xend=1990,
y = 0, yend = 0,
linewidth=2)+
scale_fill_hue(l=70, c=60)+
theme_bw(base_size = 12)+
ylab("Sommertemperatur\navvik fra 1961-1990")+
xlab("")+
guides(fill="none")+
facet_wrap( .~ factor(region, levels = regOrder),
ncol=3,
scales = "free_y")
```
Then we can take the mean and sd over the last 5 years and add to a spatial representation.
```{r}
(
i_aggregatedToPeriods <- regional_means_long %>%
mutate(year = as.numeric(year)) %>%
mutate(period = case_when(
year %between% c(2018, 2022) ~ "2018-2022",
year %between% c(2013, 2017) ~ "2013-2017",
year %between% c(2008, 2012) ~ "2008-2012",
year %between% c(2003, 2007) ~ "2003-2007",
.default = "pre 2003"
)) %>%
mutate(period_rank = case_when(
period == "2018-2022" ~ 5,
period == "2013-2017" ~ 4,
period == "2008-2012" ~ 3,
period == "2003-2007" ~ 2,
.default = 1
)) %>%
group_by(region, period, period_rank) %>%
summarise(indicator = mean(i),
sd = sd(i)
# If I inluded a spatial measure for the uncertainty, here is how I would carry the errors:
#spatial_sd = sqrt(sum(i_sd^2))/length(i_sd)
)
)
```
```{r summer-temp-time-periods, fig.cap="Scaled indicator values, aggregated over 5 year intervals. Errors represent temporal variation (standard errors) within regions and across 5 years."}
labs <- unique(i_aggregatedToPeriods$period[order(i_aggregatedToPeriods$period_rank)])
i_aggregatedToPeriods %>%
ggplot(aes(x = period_rank,
y = indicator,
colour=region))+
geom_line() +
geom_point() +
geom_errorbar(aes(ymin=indicator-sd,
ymax=indicator+sd),
width=.2,
position=position_dodge(0.2)) +
theme_bw(base_size = 12)+
scale_x_continuous(breaks = 1:5,
labels = labs)+
labs(x = "", y = "indikatorverdi")
```
Finally, I can add these values to the sp object with the accounting areas.
```{r}
reg2 <- reg %>%
left_join(i_aggregatedToPeriods[i_aggregatedToPeriods$period_rank==5,], by=join_by(region))
```
```{r wall-to-wall-summer-temp-indicator, fig.cap="Summer tempreature indicator values for five accounting areas in Norway. SD is the spatial variation."}
myCol <- "RdYlGn"
myCol2 <- "-RdYlGn"
tm_main <- tm_shape(reg2)+
tm_polygons(col="indicator",
title="Indikator:\nsommertemperatur",
palette = myCol,
style="fixed",
breaks = seq(0,1,.2))
tm_inset <- tm_shape(reg2)+
tm_polygons(col="sd",
title="SD",
palette = myCol2,
style="cont")+
tm_layout(legend.format = list(digits=2))
tmap_arrange(tm_main,
tm_inset)
```