Repository navigation
Expand file tree
/
Copy pathR_script_lab.Rmd
More file actions
180 lines (121 loc) · 5.79 KB
/
Copy pathR_script_lab.Rmd
File metadata and controls
180 lines (121 loc) · 5.79 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
---
title: "Data Manipulation"
author: "HMF"
date: "9/9/2020"
output: html_document
---
```{r setup, include=FALSE}
list.of.packages <- c("dplyr", "tidyr", "zoo", "ggplot2", "xfun", "cowplot")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_knit$set(root.dir = '.')
```
## R Markdown
```{r, warning=FALSE, message=FALSE}
library(dplyr)
library(tidyr)
library(zoo)
library(ggplot2)
```
# Read in data
We are using data from the Lamprey River sensor record (LMP 73)
To facilitate the processing, this is a subset of the full record
```{r}
lmp_subset <- read.csv("LMP_subset_sensor.csv")
head(lmp_subset)
```
pipes: %>% cntl shift m
df_cleaned <- df %>% #df_cleaned gets df which gets the filter of a dataframe
filter(...) %>% #which gets the select of that dataframe
select(...)
# Clean and reshape
```{r}
#remove columns with grab sample data
lmp_clean <- lmp_subset %>%
select(DATETIME, Site, TempC, Conductivity, SpConductivity, pH, ODOPerCent, ODOMGL, TurbidityFNU, Q, NO3_corrected, FDOM_corrected_QSU,ABS254_SUNA,Year,Month,Day, Quarter)
# or %>% select(-RECORD, -Nitrate_mg)
head(lmp_clean)
```
## group_by and summarize
generate summary tables of your dataframes
```{r}
mean_month <- lmp_clean %>%
group_by(Month) %>% # creates a semi-hidden attribute of your dataframe
summarise(meanNO3 = mean(NO3_corrected), num_obs = n())
head(mean_month)# returns NA. why? combines years also
mean_month <- lmp_clean %>%
group_by(Year, Month) %>% # creates a semi-hidden attribute of your dataframe
summarise(meanNO3 = mean(NO3_corrected, na.rm = TRUE), num_obs = n())
head(mean_month)
#in what month were nitrate concentrations and discharge highest
month_max <- lmp_clean %>%
group_by(Year, Month) %>%
summarise(NO3_max = max(NO3_corrected, na.rm = TRUE), Q_max = max(Q, na.rm=TRUE)) %>%
arrange(-NO3_max) #arrange dataframe in decreasing order
head(month_max)
```
Generate new column with values based on another column
What year had the highest summer concentration?
```{r}
# generate a season column based on quarter
lmp_clean$Season <- ifelse(lmp_clean$Quarter == "1", "Winter",
ifelse(lmp_clean$Quarter == "2", "Spring",
ifelse(lmp_clean$Quarter == "3", "Summer", "Fall")))
tail(lmp_clean)
summer_max <- lmp_clean %>%
filter(Season == "Summer") %>%
group_by(Year) %>%
summarise(NO3_max = max(NO3_corrected, na.rm = TRUE))
head(summer_max)
#simple ifelse() statement:
#qualify high and low discharge based on mean
lmp_clean$Qqual <- ifelse(lmp_clean$Q < 8000, "lower", "higher")
head(lmp_clean)
```
Calculate moving average using dplyr pipeline
```{r}
daily_lmp <- lmp_clean %>%
mutate(NO3_01da = rollapply(NO3_corrected, width = 96, FUN = mean, partial = 48), # daily
NO3_07da = rollapply(NO3_corrected, width = 672, FUN = mean, partial = 48), # weekly
NO3_30da = rollapply(NO3_corrected, width = 2880, FUN = mean, partial = 48)) # monthly
tail(daily_lmp)
```
Plot time series
```{r}
plot(x=daily_lmp$NO3_corrected, xlab="Time", ylab="NO3 (mg/L)", col="deepskyblue", type="l")
plot(x=daily_lmp$NO3_01da, xlab="Time", ylab="NO3 (mg/L)", col="deepskyblue", type="l", main="Daily moving average")
plot(x=daily_lmp$NO3_07da, xlab="Time", ylab="NO3 (mg/L)", col="darkgreen", type="l", main="Weekly moving average")
plot(x=daily_lmp$NO3_30da, xlab="Time", ylab="NO3 (mg/L)", col="black", type="l", main="Monthly moving average")
```
Plot using ggplot2
Use CQ data from WRR paper to illustrate ggplot
```{r}
lmpcq <- read.csv("LMP_30win_cq.csv")
head(lmpcq)
```
```{r}
ggplot(lmpcq, aes(y=NO3cq, x=NO3Q_rvalue )) +geom_point(col = 'blue')
```
```{r}
ggplot(lmpcq, aes(y=NO3cq, x=NO3Q_rvalue )) +geom_point(col = 'blue') +
scale_x_continuous(limits= c(-1,1)) + xlab(expression(italic(r) ~"r value")) +
ylab(expression("NO"[3]^~{} ~ "CQ"))
```
```{r}
ggplot(lmpcq, aes(y=NO3cq, x=NO3Q_rvalue, shape=as.factor(Year), col=Season )) + geom_point(size=3) +
scale_x_continuous(limits= c(-1,1)) + xlab(expression(italic(r) ~" value")) +
ylab(expression("NO"[3]^~{} ~ "CQ")) + scale_shape_manual(values = c("2012" = 4,"2013" =21, "2014" = 22, "2015" = 23, "2016" = 24, "2017" = 25)) + scale_color_manual(values= c("Fall" = "chocolate4", "Spring" = "ForestGreen", "Summer" = "goldenrod2", "Winter" = "darkblue"))
```
Change them to customize look.
```{r}
library(cowplot)
ggplot(lmpcq, aes(y=NO3cq, x=NO3Q_rvalue, shape=as.factor(Year), col=Season )) + geom_point(size=3) +
scale_x_continuous(limits= c(-1,1)) + xlab(expression(italic(r) ~" value")) +
ylab(expression("NO"[3]^~{} ~ "CQ")) + scale_shape_manual(values = c("2012" = 4,"2013" =21, "2014" = 22, "2015" = 23, "2016" = 24, "2017" = 25)) + scale_color_manual(values= c("Fall" = "chocolate4", "Spring" = "ForestGreen", "Summer" = "goldenrod2", "Winter" = "darkblue")) + theme_cowplot() + theme(legend.position = "none")
```
```{r}
ggplot(lmpcq, aes(y=NO3cq, x=NO3Q_rvalue, shape=as.factor(Year), col=Season )) + geom_point(size=3) +
scale_x_continuous(limits= c(-1,1)) + xlab(expression(italic(r) ~" value")) +
ylab(expression("NO"[3]^~{} ~ "CQ")) + scale_shape_manual(values = c("2012" = 4,"2013" =21, "2014" = 22, "2015" = 23, "2016" = 24, "2017" = 25)) + scale_color_manual(values= c("Fall" = "chocolate4", "Spring" = "ForestGreen", "Summer" = "goldenrod2", "Winter" = "darkblue")) + theme_cowplot() + theme(legend.position = "none") + annotate("rect", ymin=-0.2, ymax=0.2, xmin=-Inf, xmax=Inf, alpha=0.1) + annotate("rect", xmin=-0.3, xmax=0.3, ymin=-Inf, ymax=Inf, alpha=0.1, col="blue", fill="blue") + annotate("text", label="A", x=-1,y=2)
```