library(ggplot2)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyr)
library(here)
## here() starts at C:/Users/sbrei/Documents/R_Projects/chapter_one
library(magrittr)
##
## Attaching package: 'magrittr'
## The following object is masked from 'package:tidyr':
##
## extract
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v tibble 3.1.4 v stringr 1.4.0
## v readr 2.0.2 v forcats 0.5.1
## v purrr 0.3.4
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x magrittr::extract() masks tidyr::extract()
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
## x purrr::set_names() masks magrittr::set_names()
options(stringsAsFactors = FALSE)
weather <-
read.csv(here::here("./Meteorology/DailyData_OakvilleTwn_2018_2019.csv"),
header=T,
na.strings=c("","NA"))
weather %<>%
dplyr::rename(.,
"Lat" = 1,
"Long" = 2,
"Max.Temp_C" = 10,
"Min.Temp_C" = 12,
"Mean.Temp_C" = 14,
"Total.Precip" = 24) %>%
dplyr::select(c(1:3, 5:8, 10, 12, 14, 24 ))
boxplot(weather[,8:11], las=2)
lines(c(0,15), c(0,0))
sum(is.na(weather$Max.Temp_C)) # 1 day: 6/18/2018
## [1] 1
sum(is.na(weather$Min.Temp_C)) # 1 day: 6/18/2018
## [1] 1
sum(is.na(weather$Mean.Temp_C)) # 2 days: 6/18/2018 & 6/19/2018
## [1] 2
sum(is.na(weather$Total.Precip)) # 1 day: 6/18/2018
## [1] 1
# Since there's only 2 days without full data, remove those days from dataset
weather %<>%
filter(is.na(weather$Mean.Temp_C) == F)
monthly_data <- weather %>%
dplyr::group_by(Year, Month) %>% # group by the month column
dplyr::summarise(n=n(),
total_precip=sum(Total.Precip),
mean_temp=mean(Mean.Temp_C),
mean_max_temp = mean(Max.Temp_C),
mean_min_temp = mean(Min.Temp_C))
## `summarise()` has grouped output by 'Year'. You can override using the `.groups` argument.
head(monthly_data)
## # A tibble: 6 x 7
## # Groups: Year [1]
## Year Month n total_precip mean_temp mean_max_temp mean_min_temp
## <int> <int> <int> <dbl> <dbl> <dbl> <dbl>
## 1 2018 1 31 76.8 -4.93 -0.355 -9.5
## 2 2018 2 28 77.5 -1.12 3.34 -5.57
## 3 2018 3 31 36.5 0.442 4.34 -3.45
## 4 2018 4 30 174. 3.95 8.38 -0.5
## 5 2018 5 31 65.4 17.3 23.1 11.5
## 6 2018 6 28 90.2 19.6 24.3 14.8
monthly_data$Year <- as.factor(monthly_data$Year)
# Missing full days:
# - 2 days in June 2018
# - This will affect total precipitation data more than other data where means are used
weather %<>%
mutate(Quarter = case_when(Year == 2018 & Month < 4 ~ 1,
Year == 2018 & Month < 7 ~ 2,
Year == 2018 & Month < 10 ~ 3,
Year == 2018 & Month < 13 ~ 4,
Year == 2019 & Month < 4 ~ 5,
Year == 2019 & Month < 7 ~ 6,
Year == 2019 & Month < 10 ~ 7,
Year == 2019 & Month < 13 ~ 8) )
quarterly_data <- weather %>%
dplyr::group_by(Quarter) %>% # group by the quarter column
dplyr::summarise(n=n(),
year = first(Year),
total_precip=sum(Total.Precip),
mean_temp=mean(Mean.Temp_C),
mean_max_temp = mean(Max.Temp_C),
mean_min_temp = mean(Min.Temp_C))
head(quarterly_data)
## # A tibble: 6 x 7
## Quarter n year total_precip mean_temp mean_max_temp mean_min_temp
## <dbl> <int> <int> <dbl> <dbl> <dbl> <dbl>
## 1 1 90 2018 191. -1.89 2.41 -6.19
## 2 2 89 2018 330. 13.5 18.5 8.51
## 3 3 92 2018 262. 22.5 27.4 17.6
## 4 4 92 2018 253. 4.30 7.64 0.935
## 5 5 90 2019 213. -3.32 1.12 -7.74
## 6 6 91 2019 314. 12.2 17.2 7.15
growing_seasons <- quarterly_data %>%
dplyr::filter(Quarter == 2 |
Quarter == 3 |
Quarter == 6 |
Quarter == 7) %>%
dplyr::group_by(year) %>%
dplyr::summarise(mean_precip = mean(total_precip),
mean_temp = mean(mean_temp))
# % change in precip from 2018-2019: 19% decrease
(growing_seasons[2,2]-growing_seasons[1,2])/growing_seasons[1,2]
## mean_precip
## 1 -0.1941813
# % change in temp from 2018-2019: 8% decrease
(growing_seasons[2,3]-growing_seasons[1,3])/growing_seasons[1,3]
## mean_temp
## 1 -0.07512507
# line graph
line_precip <- monthly_data %>%
ggplot(aes(x = Month,
y = total_precip,
color = Year,
linetype = Year)) +
geom_line(size = 1) +
ylab("Total precipitation (mm)") +
scale_x_discrete(name ="Month",
limits=c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec")) +
ylim(0, 200) +
scale_color_grey(start = 0.4, end = 0.6)+
theme_bw()
# export
ggsave(here::here("./Figures_Tables/Meteorology/Monthly_precip_linegraph.png"),
width = 18, height = 8, units = "cm")
# bar graph
monthly_data %>%
ggplot(aes(x = Month,
y = total_precip,
fill = Year)) +
geom_bar(position="dodge", stat = "identity") +
ylab("Total precipitation (mm)") +
scale_x_discrete(name ="Month",
limits=c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec")) +
ylim(0, 200) +
scale_fill_grey(start = 0.4, end = 0.6)+
theme_bw()
# export
ggsave(here::here("./Figures_Tables/Meteorology/Monthly_precip_bargraph.png"),
width = 18, height = 8, units = "cm")
# line graph
line_temp <- monthly_data %>%
ggplot(aes(x = Month,
y = mean_temp,
color = Year,
linetype = Year)) +
geom_line(size = 1) +
ylab("Mean temperature (C)") +
scale_x_discrete(name ="Month",
limits=c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec")) +
ylim(-10, 30) +
scale_color_grey(start = 0.4, end = 0.6)+
theme_bw()
# export
ggsave(here::here("./Figures_Tables/Meteorology/Monthly_temp_linegraph.png"),
width = 18, height = 8, units = "cm")
# bar graph
monthly_data %>%
ggplot(aes(x = Month,
y = mean_temp,
fill = Year)) +
geom_bar(position="dodge", stat = "identity") +
ylab("Mean temperature (C)") +
scale_x_discrete(name ="Month",
limits=c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec")) +
ylim(-10, 30) +
scale_fill_grey(start = 0.4, end = 0.6)+
theme_bw()
# export
ggsave(here::here("./Figures_Tables/Meteorology/Monthly_temp_bargraph.png"),
width = 18, height = 8, units = "cm")
library(patchwork)
line_precip / line_temp + plot_annotation(tag_levels = 'A')
# export
ggsave(here::here("./Figures_Tables/Meteorology/Combined_linegraph.png"),
width = 21, height = 16, units = "cm")
write.csv(monthly_data, here::here("./Figures_Tables/Meteorology/Monthly_data.csv"), row.names=TRUE)
write.csv(quarterly_data, here::here("./Figures_Tables/Meteorology/Quarterly_data.csv"), row.names=TRUE)