library(tidyverse)
library(lubridate)
#new this week
library(rnoaa)
library(trend)
climate_data <- ghcnd_search(
stationid = "USC00053005",
date_min = "1988-01-01",
date_max = "2020-12-31",
var = c("SNOW", "TMAX", "TMIN", "PRCP", "TOBS", "SNWD")
)
variable_reducer <- function(df){
min_data = dplyr::select(df, -id, -mflag, -qflag, -sflag)
}
relevant_data <- climate_data %>%
map(., variable_reducer)
clim_dat <- reduce(relevant_data, full_join, by = c("date")) %>%
arrange(date) %>%
mutate_if(is.numeric, vars(./10))
ggplot(clim_dat, aes(x = date, y = tmax, color = date)) +
geom_point() +
ylab("Maximum temperature (C)") +
xlab("Date") +
theme_linedraw(base_size = 16) +
scale_color_gradient(low = 'cyan', high = 'deeppink', trans = "date")
ggplot(clim_dat, aes(x = date, y = tmax, color = date)) +
geom_point() +
ylab(expression(Maximum~temperature~(""^o*C))) +
xlab("Date") +
theme_linedraw(base_size = 16) +
scale_color_gradient(low = 'cyan', high = 'deeppink', trans = "date")
ggplot(clim_dat, aes(x = date, y = tmin, color = date)) +
geom_point() +
xlab("Year") +
ylab("Minimum temperature (C)") +
theme_linedraw(base_size = 16) +
scale_color_gradient(low = "cyan", high = "deeppink", trans = "date") +
stat_smooth(method = "lm", color = "red")
myd_dat <- clim_dat %>%
mutate(month = month(date),
year = year(date),
day = day(date))
m_avg <- myd_dat %>%
filter(month == 9) %>%
group_by(month, year) %>%
summarize(m_tmin = mean(tmin))
days_tmin <- myd_dat %>%
filter(day %in% 16:20) %>%
group_by(month, day) %>%
summarize(max_tmin = max(tmin, na.rm = TRUE),
min_tmin = min(tmin, na.rm = TRUE),
avg_tmin = mean(tmin, na.rm = TRUE))
monitor_days <- myd_dat %>%
filter(day %in% 16:20) %>%
filter(month == 8) %>%
group_by(month, day) %>%
summarize(max_tmin = max(tmin, na.rm = TRUE),
min_tmin = min(tmin, na.rm = TRUE),
avg_tmin = mean(tmin, na.rm = TRUE),
avg_p = mean(prcp, na.rm = TRUE),
max_p = max(prcp, na.rm = TRUE),
)
t_min_test <- sens.slope(m_avg$m_tmin)
ggplot(m_avg, aes(x = year, y = m_tmin, color = year)) +
geom_point() +
geom_line() +
xlab("") +
ylab(expression(Average~minimum~temperature~(""^o*C))) +
theme_linedraw(base_size = 16) +
stat_smooth (method = lm) +
stat_smooth (method = lm) +
scale_color_gradient(low = "blue", high = "red") +
annotate("text", x = 1995, y = 12, label = paste("p = ", round(t_min_test$p.value, digits = 3), "\n",
"Sen's slope = ", round(t_min_test$estimates, digits = 2)))
`