-
Notifications
You must be signed in to change notification settings - Fork 0
/
DOY_temperaturetrends_threshold21.Rmd
155 lines (132 loc) · 5.57 KB
/
DOY_temperaturetrends_threshold21.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: "RegionDaysExceedanceComparison"
author: "Catarina Pien"
date: "4/13/2022"
output: html_document
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(tidyverse)
library(lubridate)
library(grid)
library(gridExtra)
library(viridis)
library(ggpubr)
library(scales)
```
```{r}
#tempdata <- readRDS("Data/temp10years_20200922.rds")
tempDaily <- readRDS("Data/tempDaily_20210811.rds") %>%
mutate(Region = case_when(Region == "San Joaquin"~ "Central",
Region == "Sac River"~ "Confluence",
TRUE~ Region)) %>%
mutate(Month = month(Date),
Year = year(Date),
DOY = yday(Date),
Season = case_when(Month %in% c(1,2,3) ~ "Winter",
Month %in% c(4,5,6) ~ "Spring",
Month %in% c(7,8,9) ~ "Summer",
Month %in% c(10,11,12) ~ "Fall",
TRUE~as.character(NA)))
LatLon <- read.csv(here::here("Data/StationsMetadata.csv"))
```
## Make new dataset for 2015, calculate whether suitable and percent of each day that is suitable
```{r}
temp_Region <- tempDaily %>%
group_by(Region, Date, Year, Season, DOY, Month) %>%
summarize(meanMax = mean(maxDaily),
meanMean = mean(meanDaily),
meanMin = mean(minDaily)) %>%
ungroup()
temp_filt <- temp_Region %>%
filter(Year %in% c(2015, 2018)) %>%
mutate(Date2 =date(paste0("1998-", month(Date), "-", day(Date) ))) %>%
mutate(Suitable_max = ifelse(meanMax<21, 1, 0),
Suitable_min = ifelse(meanMin<21, 1, 0),
DiffMax = 21-meanMax,
DiffMin = 21-meanMin) %>%
group_by(Region) %>%
mutate(Suitable = sum(Suitable_max == 1)/n()) %>%
ungroup()%>%
mutate(Suitable_min = factor(Suitable_min),
Suitable_max = factor(Suitable_max)) %>%
mutate(Year = ifelse(Year == 2015, "2015 (Hot Year)", "2018 (Average Year)"),
Region = factor(Region, levels = c("Suisun Marsh", "Suisun Bay","South", "Central","Confluence","North Delta")))
levels(temp_filt$Region)
```
```{r}
theme_fig <- theme_bw() +
ggplot2::theme(plot.title=element_text(size=11),
axis.text.x=element_text(size=13, color="black"),
axis.text.y = element_text(size=13, color="black",angle=45),
axis.title.y = element_text(size=15, color="black"),
strip.text = element_text(size = 13),
legend.text=element_text(size = 13),
strip.background = element_rect(size=0.3))
```
```{r}
ggplot(temp_filt) +
geom_tile(aes(DOY, Region, fill = DiffMin)) +
scale_fill_steps2(midpoint=0, n.breaks=8, low = "tomato3", high = "darkblue")+
geom_line(data = data.frame(x = c(0, 365) + 0.5, y = rep(2:6, each = 2) - 0.5),
aes(x = x, y = y, group = y)) +
scale_colour_manual(values = c("navy", "navajowhite3")) +
scale_x_continuous(breaks = seq(0,365, 30)) +
facet_wrap(~Year,nrow = 2) +
theme_fig
months = as.Date(c("1998-01-01", "1998-02-01", "1998-03-01", "1998-04-01","1998-05-01", "1998-06-01","1998-07-01", "1998-08-01","1998-09-01", "1998-10-01","1998-11-01", "1998-12-01", "1998-12-31"))
(maxplot <- ggplot(temp_filt) +
geom_tile(aes(Date2, Region, fill = DiffMax)) +
scale_fill_steps2(midpoint=0, n.breaks=12, low = "tomato3", high = "darkblue")+
geom_vline(xintercept = months, linetype = "longdash") +
geom_line(data =
data.frame(x = as.Date(c("1998-01-01", "1998-12-31")) + 0.5,
y = rep(2:6, each = 2) - 0.5),
aes(x = x, y = y, group = y)) +
# geom_line(data = data.frame(x = c(0, 365) + 0.5, y = rep(2:6, each = 2) - 0.5),
# aes(x = x, y = y, group = y)) +
scale_colour_manual(values = c("navy", "navajowhite3")) +
scale_x_date(date_breaks = "1 month", date_labels = "%b",
expand = c(0,0)) +
scale_y_discrete(expand = c(0,0))+
facet_wrap(~Year,nrow = 2) +
labs(fill = "Difference between \nregional mean maximum \ntemperature and 21°C") +
theme_fig + theme(axis.title.y = element_blank(),
axis.title.x = element_blank(),
legend.position = "top",
legend.text = element_text(size = 11),
plot.margin = margin(0,0.2,0.5,0.2, "in")) +
guides(fill = guide_colourbar( barwidth = 15)))
```
```{r}
png(filename=file.path("Figures/MaxTempComparisonYears.png"), units="in",type="cairo", bg="white", height=6,
width=9, res=300, pointsize=10)
maxplot
dev.off()
```
Look at Station differences
```{r}
tempInfo <- tempDaily %>%
group_by(Region, Station) %>%
summarize(n = length(unique(WY))) %>%
left_join(LatLon)
LatLon <- read.csv("Data/StationsMetadata.csv")
LatLon_sf <- st_as_sf(tempInfo, coords = c("Longitude", "Latitude"), crs = 4326)
regions <- st_read("RosieRegions/RosiesRegions2022.shp")
regions$Region <- c("Suisun Marsh", "Suisun Bay","Confluence", "North Delta", "Far North", "Central" , "South")
stations_sf <- st_as_sf(tempInfo, coords = c("Longitude", "Latitude"), crs = 4326) %>%
mutate(Region = replace(Region, Region == "Sac River", "Confluence"),
Region = replace(Region, Region == "San Joaquin", "Central"))
regions_4326 <- st_transform(regions, crs = st_crs(stations_sf))
mapview::mapview(stations_sf, zcol = "Station")
mapview::mapview(LatLon_sf, zcol = "Station")
tempDaily %>%
group_by(WY, Station) %>%
mutate(sd = sd(meanDaily)) %>%
filter(Region == "North Delta") %>%
ggplot() + geom_boxplot(aes(WY, sd, fill =factor(Station)))
```