-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
168 lines (126 loc) · 5 KB
/
README.Rmd
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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r setup, include = FALSE}
knitr::opts_chunk$set(
fig.path = "man/figures/README-",
message = FALSE,
warning = FALSE,
collapse = TRUE,
comment = "#>",
dev = "ragg_png",
dpi = 300,
out.width = "100%"
)
```
# ldc
<!-- badges: start -->
[![Project Status: WIP – Initial development is in progress, but there has not yet been a stable, usable release suitable for the public.](https://www.repostatus.org/badges/latest/wip.svg)](https://www.repostatus.org/#wip)
[![R-CMD-check](https://github.com/TxWRI/ldc/actions/workflows/check-standard.yaml/badge.svg)](https://github.com/TxWRI/ldc/actions/workflows/check-standard.yaml)
[![license](https://img.shields.io/badge/license-MIT + file LICENSE-lightgrey.svg)](https://choosealicense.com/)
[![Codecov test coverage](https://codecov.io/gh/TxWRI/ldc/branch/main/graph/badge.svg)](https://app.codecov.io/gh/TxWRI/ldc?branch=main)
<!-- badges: end -->
ldc provides automated and fairly opinionated functions for generating pollutant load duration curves (LDCs) in freshwater streams. Due to the automated nature, there isn't much ability to adjust methodology or customize the generated LDCs since much of the calculation is abstracted away from the user.
ldc has three major functions:
- `calc_ldc` takes and input dataset of matched flow and pollutant concentrations to generate a table with exceedance probabilities grouped by user specified break points.
- `summ_ldc` uses the output from `calc_ldc` to generate a summary dataframe
- `draw_ldc` uses the output from both functions to generate a LDC figure as a ggplot object.
## Installation
ldc is currently on Github. First install the remotes package then install ldc from Github:
``` r
remotes::install_github("TxWRI/ldc")
```
## Example
An example using the data in the ldc package is shown below.
### Setup and format data
```{r data}
library(ldc)
library(dplyr)
library(units)
library(ggplot2)
## this will calculate a ldc for indicator bacteria
## ldc uses the unit package to facilitate unit conversions
## we need to make the cfu unit first, since it isn't included
## in the units package
install_unit("cfu")
## format the data for use in ldc
tres_palacios <- as_tibble(tres_palacios) |>
## flow must have units, here is is in cfs
mutate(Flow = set_units(Flow, "ft^3/s"))|>
## pollutant concentration must have units
mutate(Indicator_Bacteria = set_units(Indicator_Bacteria, "cfu/100mL"))
tres_palacios
```
**Calculate exceedance probability**
```{r calcldc}
## specify the allowable concentration
allowable_concentration <- 126
## set the units
units(allowable_concentration) <- "cfu/100mL"
## calculate the exceedance probabilities along with
## allowable pollutant loads and measured pollutant loads
## at given probabilities
df_ldc <- calc_ldc(tres_palacios,
Q = Flow,
C = Indicator_Bacteria,
allowable_concentration = allowable_concentration)
df_ldc
```
**Summarize data**
```{r summldc}
df_sum <- summ_ldc(df_ldc,
Q = Flow,
C = Indicator_Bacteria,
Exceedance = P_Exceedance,
groups = Flow_Category,
method = "geomean")
df_sum
```
**Plot LDC**
```{r ldcplot}
draw_ldc(df_ldc,
df_sum,
y_lab = expression(paste(italic("E. coli"))),
label_nudge_y = log10(1000)) +
scale_y_log10() +
annotation_logticks(sides = "l") +
theme_bw() +
theme(legend.position = "bottom",
legend.title = element_blank(),
legend.direction = "vertical",
panel.grid = element_blank())
```
### Units
ldc relies on the units package to facilitate unit conversions and tracking of units across variables. This is handy if we want to transform units on the fly. In the above summary table, median daily flow volume is reported in units of 100mL/day. This isn't a logical unit to communicate, lets change it to million. gallons/day.
```{r unitconv1}
df_sum |>
mutate(Median_Daily_Flow_Volume = set_units(Median_Daily_Flow_Volume, "1E6gallons/day")) -> df_sum
df_sum
```
cfu/day is a really big number. We can convert that to billion cfu/day.
```{r unitconv2}
df_sum |>
mutate(Median_Flow_Load = set_units(Median_Flow_Load, "1E9cfu/day")) -> df_sum
df_sum
```
If we want to plot these, we also need to convert the df_ldc variables to matching units.
```{r unitconv3}
df_ldc |>
mutate(Daily_Load = set_units(Daily_Load, "1E9cfu/day"),
Allowable_Daily_Load = set_units(Allowable_Daily_Load, "1E9cfu/day")) -> df_ldc
```
Updated units will carry over to the plot:
```{r ldcplot2}
draw_ldc(df_ldc,
df_sum,
y_lab = expression(paste(italic("E. coli"))),
label_nudge_y = log10(1000)) +
scale_y_log10() +
annotation_logticks(sides = "l") +
theme_bw() +
theme(legend.position = "bottom",
legend.title = element_blank(),
legend.direction = "vertical",
panel.grid = element_blank())
```