-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHistoricMetData.R
122 lines (104 loc) · 4.67 KB
/
HistoricMetData.R
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
## Plotting Temperature Distributions from the Field Seln Expeiments from 1999
# Taylor M. Hatcher
#
#
#
#
# install required packages
install.packages("readxl")
# Load required libraries
library(data.table)
library(ggplot2)
library(lubridate)
library(dplyr)
library(tidyr)
library(readxl)
library(viridis)
# set working directory to github repository
setwd("~/Desktop/Repos/WARP2024/Data")
# Load data and exclude the first rows but keep header names
Colnames <- as.character(read_xlsx("UWCUH.MetData.Seln2.Aug1999.jdatecorrected.Taylorsversion.xlsx", n_max = 1, col_names = FALSE))
histmicroclimdata <- read_xlsx(path = "UWCUH.MetData.Seln2.Aug1999.jdatecorrected.Taylorsversion.xlsx", skip = 7, col_names = Colnames)
# Select for columns needed for analysis - excluding TM13 and TM10 because logger seems to have been malfunctioning during some of the recording time
histmicroclimdata <- histmicroclimdata %>%
select("YEAR", "SITE","DATE", "JDATE", "LONGTIME", "TIME", "SOLAR", "TAIR","TM1","TM2","TM3","TM4","TM5","TM6","TM7","TM8","TM9","TM11","TM12","TM14","TM15","TM16","TM17","TM18","TM19","TM20")
# Convert LONGTIME to a proper datetime format
histmicroclimdata$LONGTIME <- as.POSIXct(histmicroclimdata$LONGTIME, format = "%Y-%m-%d %H:%M:%S")
# Extract only the time in HH:MM format (when I uploaded excel it inputted incorrect dates in the longtime column)
histmicroclimdata$LONGTIME <- format(histmicroclimdata$LONGTIME, "%H:%M")
# Combine DATE and LONGTIME into a single datetime column
histmicroclimdata <- histmicroclimdata %>%
mutate(
DATETIME = paste(DATE, LONGTIME), # Combine DATE and LONGTIME
DATETIME = ymd_hm(DATETIME) # Parse combined column as datetime
)
# Reshape data into long format for TM1 to TM20 to help with visualization
histmicroclimdata_long <- histmicroclimdata %>%
pivot_longer(
cols = starts_with("TM"), # Select all columns that start with "TM"
names_to = "Variable",
values_to = "Value"
)
write.csv(histmicroclimdata_long,"FormattedHistoricMetDataFieldSln.csv")
# Plot all TM columns as separate lines
AllTemperaturePlot <- ggplot(histmicroclimdata_long, aes(x = DATETIME, y = Value, color = Variable)) +
geom_point() +
scale_color_viridis_d()+
labs(
title = "Historic 1999 Field Seln Microclimate Temperatures",
x = "Datetime",
y = "Temperature (°C)",
color = "Sensor"
) +
theme_minimal()
#print Temperature plot plot
print(AllTemperaturePlot)
# Calculate temp extremes for data frame
temp_extremes <- histmicroclimdata_long %>%
group_by(Variable) %>%
summarise(T_min = min(Value, na.rm = TRUE),
T_max = max(Value, na.rm = TRUE),
.groups = 'drop')
# Reshape the temp_extremes data into long format for plotting
temp_extremes_long <- temp_extremes %>%
pivot_longer(cols = c(T_min, T_max), names_to = "Temperature_Type", values_to = "Temperature")
# Min and Max distribution plot for each temperature logger
histminmaxplot <- ggplot(temp_extremes_long, aes(x = Variable, y = Temperature, fill = Temperature_Type)) +
geom_bar(stat = "identity", position = position_dodge()) +
scale_fill_viridis_d() +
labs(
title = "1999 Field Seln Minimum and Maximum Temperatures by Logger",
x = "Logger",
y = "Temperature (°C)",
fill = "Temperature Type"
) +
theme_classic(base_size = 16) +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
print(histminmaxplot)
# Combine DATE and LONGTIME into a single datetime column
histhourly_means <- histmicroclimdata_long %>%
mutate(HourlyTime = floor_date(DATETIME, "hour")) %>%
group_by(HourlyTime, Variable) %>%
summarise(MeanTemperature = mean(Value, na.rm = TRUE),
.groups = 'drop')
# Calculate hourly means for each temperature logger
histhourly_means <- histmicroclimdata_long %>%
mutate(HourlyTime = floor_date(DATETIME, "hour")) %>% # Round datetime to the nearest hour
group_by(HourlyTime, Variable) %>% # Group by rounded hour and logger
summarise(MeanTemperature = mean(Value, na.rm = TRUE), .groups = 'drop') # Calculate mean
# Plot hourly means for each temperature logger in the field
hourly_plot <- ggplot(histhourly_means, aes(x = HourlyTime, y = MeanTemperature, color = Variable)) +
geom_line() + # Use line plot to show trends
scale_color_viridis_d() +
labs(
title = "1999 Field Seln Hourly Mean Temperatures by Logger",
x = "Datetime",
y = "Mean Temperature (°C)",
color = "Sensor"
) +
theme_minimal() + # Clean plot style
theme(
axis.text.x = element_text(angle = 45, hjust = 1) # Rotate x-axis labels for better readability
)
# Print the hourly data plot
print(hourly_plot)