Load data with daily max and min temps for SoCal zip codes (gridMET 2006-2021)
load("~/UCSD-Scripps/R Files 2022/Heat Metrics/socal_heat.RData")
socal_heat$tmmx_C <- socal_heat$tmmx - 273.15
socal_heat$tmmn_C <- socal_heat$tmmn - 273.15
library(dplyr);
socal_heat <- socal_heat %>% select(-one_of('tmmx', 'tmmn')) # remove columns with temp in Kelvin
summary(socal_heat)
date zip tmmx_C tmmn_C
Min. :2006-01-01 Min. :90001 Min. :-8.142 Min. :-20.850
1st Qu.:2010-01-01 1st Qu.:91205 1st Qu.:19.702 1st Qu.: 8.204
Median :2013-12-29 Median :92114 Median :24.024 Median : 11.994
Mean :2013-12-30 Mean :91908 Mean :24.504 Mean : 11.911
3rd Qu.:2017-12-30 3rd Qu.:92630 3rd Qu.:29.032 3rd Qu.: 15.985
Max. :2021-12-31 Max. :93591 Max. :50.987 Max. : 37.250
socal_heat$year <- format(socal_heat$date, "%Y")
Find the 95th percentiles for max temp for each zip code and year
temp95 <- socal_heat %>%
group_by(zip) %>%
mutate(max95 = quantile(tmmx_C, probs = .95))
summary(temp95)
date zip tmmx_C tmmn_C year max95
Min. :2006-01-01 Min. :90001 Min. :-8.142 Min. :-20.850 Length:4392384 Min. :24.85
1st Qu.:2010-01-01 1st Qu.:91205 1st Qu.:19.702 1st Qu.: 8.204 Class :character 1st Qu.:30.56
Median :2013-12-29 Median :92114 Median :24.024 Median : 11.994 Mode :character Median :34.20
Mean :2013-12-30 Mean :91908 Mean :24.504 Mean : 11.911 Mean :34.04
3rd Qu.:2017-12-30 3rd Qu.:92630 3rd Qu.:29.032 3rd Qu.: 15.985 3rd Qu.:37.12
Max. :2021-12-31 Max. :93591 Max. :50.987 Max. : 37.250 Max. :45.72
temp95$heat95 <- ifelse(temp95$tmmx_C >= temp95$max95, 1, NA ) # assign "1" when max temp >= 95th
Create index of events where max temp > = 95th max temp (for 1st and 2nd heat waves, only)
heatwave <- temp95 %>%
group_by(zip, year, heat95) %>%
arrange(date) %>%
mutate(Seq = ifelse(!is.na(heat95), 1:n(), NA))
heatwave$hw_event <- ifelse(heatwave$Seq == 1 | heatwave$Seq == 2, heatwave$Seq, NA )
summary(heatwave)
date zip tmmx_C tmmn_C year max95
Min. :2006-01-01 Min. :90001 Min. :-8.142 Min. :-20.850 Length:4392384 Min. :24.85
1st Qu.:2010-01-01 1st Qu.:91205 1st Qu.:19.702 1st Qu.: 8.204 Class :character 1st Qu.:30.56
Median :2013-12-29 Median :92114 Median :24.024 Median : 11.994 Mode :character Median :34.20
Mean :2013-12-30 Mean :91908 Mean :24.504 Mean : 11.911 Mean :34.04
3rd Qu.:2017-12-30 3rd Qu.:92630 3rd Qu.:29.032 3rd Qu.: 15.985 3rd Qu.:37.12
Max. :2021-12-31 Max. :93591 Max. :50.987 Max. : 37.250 Max. :45.72
heat95 Seq hw_event
Min. :1 Min. : 1 Min. :1
1st Qu.:1 1st Qu.: 5 1st Qu.:1
Median :1 Median :11 Median :1
Mean :1 Mean :13 Mean :1
3rd Qu.:1 3rd Qu.:18 3rd Qu.:2
Max. :1 Max. :78 Max. :2
NA's :4171714 NA's :4171714 NA's :4369215
Plot a ZIP code
lajolla <- heatwave[heatwave$zip == 92037 & heatwave$date >= "2017-01-01", ] # one zip code, last 5 years
unique(lajolla$max95)
[1] 27.1006
library(ggplot2)
ggplot(data = lajolla, aes(x = date, y = hw_event)) +
geom_bar(stat = "identity", fill = "red", width = 5) +
geom_text(aes(label=round(tmmx_C, 1)), vjust=1.6, hjust = 1, color="black", size=3.5)+
scale_x_date(date_breaks = "3 month") +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(x = "", y = "N Heatwave", title = "La Jolla 92037 (2017-2021) \nHeatwave Event (max temp >= 95th)")