library(readxl)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.3.0
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(fs)
##############################################
# Function: climatets
##############################################
climatets <- function(filename, num, plot = TRUE, s = 2) {
##############################################
# function to set up temp data for climate series
# inputs: filename and sheet number
# output: returns cleaned data and plot
# written by: P. Kohli
# date: 4/13/24
##############################################
dataexcel <- read_excel(filename, sheet = num, range = "D7:H77")
dataexcel <- dataexcel[-1, -2]
dataexcel <- filter(dataexcel, !is.na(dataexcel$Avg))
data <- dataexcel %>% mutate(Month = num)
if (plot == TRUE) {
g <- data %>% ggplot(aes(Year, Avg)) + geom_line(size = s)
return(list(data, g))
} else {
return(data)
}
}
Jan.NewHaven <- climatets("Climate_Northeast_NewHavenCT.xlsx", 1, FALSE)
## New names:
## • `` -> `...2`
Jan.Warwick <- climatets("Climate_Northeast_WarwickRI.xlsx", 1, FALSE)
## New names:
## • `` -> `...2`
Jan.Worcester <- climatets("Climate_Northeast_WorcesterMA 6-16-19.xlsx", 1, FALSE)
## New names:
## • `` -> `...2`
Jan.NewHaven <- Jan.NewHaven %>% mutate(across(c(Year, Max, Min, Avg), as.numeric))
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `across(c(Year, Max, Min, Avg), as.numeric)`.
## Caused by warning:
## ! NAs introduced by coercion
Jan.Warwick <- Jan.Warwick %>% mutate(across(c(Year, Max, Min, Avg), as.numeric))
Jan.Worcester <- Jan.Worcester %>% mutate(across(c(Year, Max, Min, Avg), as.numeric))
#remove missing values
Jan.NewHaven.nna <- Jan.NewHaven %>% filter(!is.na(Avg))
Jan.Warwick.nna <- Jan.Warwick %>% filter(!is.na(Avg))
Jan.Worcester.nna <- Jan.Worcester %>% filter(!is.na(Avg))
#ii
#Interactive plots
library(plotly)
##
## Attaching package: 'plotly'
##
## The following object is masked from 'package:ggplot2':
##
## last_plot
##
## The following object is masked from 'package:stats':
##
## filter
##
## The following object is masked from 'package:graphics':
##
## layout
#New Haven, CT
p_newhaven <- plot_ly(Jan.NewHaven.nna, x = ~Year) %>%
add_lines(y = ~Max, name = "Max", alpha = 0.6) %>%
add_lines(y = ~Min, name = "Min", alpha = 0.6) %>%
add_lines(y = ~Avg, name = "Avg", alpha = 0.8) %>%
layout(title = "New Haven, CT – January Temperature Time Series",
yaxis = list(title = "Temperature"))
p_newhaven
#Warwick, RI
p_warwick <- plot_ly(Jan.Warwick.nna, x = ~Year) %>%
add_lines(y = ~Max, name = "Max", alpha = 0.6) %>%
add_lines(y = ~Min, name = "Min", alpha = 0.6) %>%
add_lines(y = ~Avg, name = "Avg", alpha = 0.8) %>%
layout(title = "Warwick, RI – January Temperature Time Series",
yaxis = list(title = "Temperature"))
p_warwick
#Worcester, MA
p_worcester <- plot_ly(Jan.Worcester.nna, x = ~Year) %>%
add_lines(y = ~Max, name = "Max", alpha = 0.6) %>%
add_lines(y = ~Min, name = "Min", alpha = 0.6) %>%
add_lines(y = ~Avg, name = "Avg", alpha = 0.8) %>%
layout(title = "Worcester, MA – January Temperature Time Series",
yaxis = list(title = "Temperature"))
p_worcester