Skip to content

Commit 6902046

Browse files
committed
finish vignettes, modify plot args
1 parent 794cb27 commit 6902046

File tree

9 files changed

+152
-50
lines changed

9 files changed

+152
-50
lines changed

R/get_scenario.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#'
88
#' @returns a data frame with columns for id, scenario, year, SLR in meters, and SLR in feet.
99
#'
10-
#' @details Information from <https://sealevel.nasa.gov/task-force-scenario-tool?psmsl_id={gauge}>. Results are SLR in mm and feet for the intermediate low, intermediate, and intermediate high scenarios by default based on recommended scenarios from the Climate Science Advisory Panel. Full options for scenarios are `'Low'`, `'IntLow'`, `'Int'`, `'IntHigh'`, and `'High'`. Values for SLR are relative change from 2020.
10+
#' @details Information from <https://sealevel.nasa.gov/task-force-scenario-tool?psmsl_id={gauge}>. Results are SLR in meters and feet for the intermediate low, intermediate, and intermediate high scenarios by default based on recommended scenarios from the Climate Science Advisory Panel. Full options for scenarios are `'Low'`, `'IntLow'`, `'Int'`, `'IntHigh'`, and `'High'`. Values for SLR are relative change from 2020.
1111
#'
1212
#' @export
1313
#'

R/plot_scenario.R

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
#' @param units character, units for the y-axis. Default is `'ft'`. Options are `'ft'` and `'m'`.
66
#' @param linewidth numeric, line width. Default is `1`.
77
#' @param caption logical, add caption with source. Default is `TRUE`.
8-
#' @param title logical, add title. Default is `TRUE`.
9-
#' @param subtitle character, subtitle for the plot. Default is `'Gauge 8726520, St. Petersburg, FL'`. Use `NULL` to omit.
108
#' @param xrng numeric, x-axis range. Default is `c(2020, 2100)`.
11-
#' @param xbrk numeric, x-axis breaks. Default is `10`.
12-
#' @param yrng numeric, y-axis range. Default is `c(0, 6)`.
13-
#' @param ybrk numeric, y-axis breaks. Default is `1`.
9+
#' @param xbrk numeric, number of x-axis breaks. Default is `10`.
10+
#' @param yrng numeric, y-axis range as two values. Default is `NULL`, which uses the range of the data..
11+
#' @param ybrk numeric, number of y-axis breaks. Default is `7`.
1412
#'
1513
#' @returns a ggplot object
1614
#' @export
@@ -19,8 +17,7 @@
1917
#' dat <- get_scenario()
2018
#' plot_scenario(dat)
2119
plot_scenario <- function(dat, cols = c('deepskyblue', 'orange', 'red'), units = 'ft', linewidth = 1,
22-
caption = TRUE, title = TRUE, subtitle = 'Gauge 8726520, St. Petersburg, FL',
23-
xrng = c(2020, 2100), xbrk = 10, yrng = c(0, 6), ybrk = 1){
20+
caption = TRUE, xrng = c(2020, 2100), xbrk = 10, yrng = NULL, ybrk = 7){
2421

2522
units <- match.arg(units, c('ft', 'm'))
2623

@@ -35,18 +32,23 @@ plot_scenario <- function(dat, cols = c('deepskyblue', 'orange', 'red'), units =
3532
toplo <- dat |>
3633
dplyr::rename(
3734
yvl = !!colnm
38-
)
35+
) |>
36+
dplyr::filter(
37+
year >= xrng[1] & year <= xrng[2]
38+
)
3939

40+
if(is.null(yrng))
41+
yrng <- c(0, max(toplo$yvl, na.rm = TRUE))
42+
4043
p <- ggplot2::ggplot(toplo, ggplot2::aes(x = year, y = yvl, color = scenario, group = scenario)) +
4144
ggplot2::geom_line(linewidth = linewidth) +
4245
ggplot2::labs(
4346
x = NULL,
4447
y = ylab,
45-
color = NULL,
46-
subtitle = subtitle
48+
color = NULL
4749
) +
4850
ggplot2::scale_color_manual(values = colval) +
49-
ggplot2::scale_y_continuous(breaks = seq(yrng[1], yrng[2], by = ybrk), limits = yrng) +
51+
ggplot2::scale_y_continuous(breaks = scales::pretty_breaks(n = ybrk), limits = yrng) +
5052
ggplot2::scale_x_continuous(breaks = scales::pretty_breaks(n = xbrk), limits = xrng) +
5153
ggplot2::theme_minimal() +
5254
ggplot2::theme(
@@ -62,11 +64,6 @@ plot_scenario <- function(dat, cols = c('deepskyblue', 'orange', 'red'), units =
6264
ggplot2::labs(caption = cp)
6365
}
6466

65-
if(title){
66-
p <- p +
67-
ggplot2::labs(title = 'Relative Sea Level Change Predictions')
68-
}
69-
7067
return(p)
7168

7269
}

R/plot_sealevel.R

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
#' @param col character, color for the line. Default is `'deepskyblue'`.
55
#' @param units character, units for the y-axis. Default is `'ft'`. Options are `'ft'` and `'m'`.
66
#' @param caption logical, add caption with source. Default is `TRUE`.
7-
#' @param xrng Date, x-axis range. Default is `NULL`, which uses the range of the data.
8-
#' @param xbrk numeric, x-axis breaks. Default is `10`.
9-
#' @param yrng numeric, y-axis range. Default is `c(-1, 1)`.
10-
#' @param ybrk numeric, y-axis breaks. Default is `0.5`.
7+
#' @param xrng Date, x-axis range as two values. Default is `NULL`, which uses the range of the data.
8+
#' @param xbrk numeric, number of x-axis breaks. Default is `10`.
9+
#' @param yrng numeric, y-axis range as two values. Default is `NULL`, which uses the range of the data.
10+
#' @param ybrk numeric, number of y-axis breaks. Default is `3`.
1111
#'
1212
#' @returns a ggplot object
1313
#' @export
1414
#'
1515
#' @examples
1616
#' dat <- get_sealevel()
1717
#' plot_sealevel(dat)
18-
plot_sealevel <- function(dat, col = 'deepskyblue', units = 'ft', caption = TRUE, xrng = NULL, xbrk = 10, yrng = c(-1, 1), ybrk = 0.5) {
18+
plot_sealevel <- function(dat, col = 'deepskyblue', units = 'ft', caption = TRUE, xrng = NULL, xbrk = 10, yrng = NULL, ybrk = 3) {
1919

2020
units <- match.arg(units, c('ft', 'm'))
2121

@@ -24,24 +24,29 @@ plot_sealevel <- function(dat, col = 'deepskyblue', units = 'ft', caption = TRUE
2424
}
2525
if(!is.null(xrng))
2626
stopifnot(inherits(xrng, 'Date'))
27-
27+
2828
ylab <- ifelse(units == 'ft', 'Feet (MSL)', 'Meters (MSL)')
29-
ybrks <- seq(yrng[1], yrng[2], by = ybrk)
3029

3130
colnm <- names(dat)[grepl(paste0('msl_', units), names(dat))]
3231

3332
toplo <- dat |>
3433
dplyr::rename(
3534
yvl = !!colnm
36-
)
35+
) |>
36+
dplyr::filter(
37+
date >= xrng[1] & date <= xrng[2]
38+
)
3739

40+
if(is.null(yrng))
41+
yrng <- range(toplo$yvl, na.rm = TRUE)
42+
3843
p <- ggplot2::ggplot(toplo, ggplot2::aes(x = date, y = yvl)) +
3944
ggplot2::geom_line(color = col) +
4045
ggplot2::labs(
4146
x = NULL,
4247
y = ylab
4348
) +
44-
ggplot2::scale_y_continuous(breaks = ybrks, limits = yrng) +
49+
ggplot2::scale_y_continuous(breaks = scales::pretty_breaks(n = ybrk), limits = yrng) +
4550
ggplot2::scale_x_date(breaks = scales::pretty_breaks(n = xbrk), limits = xrng) +
4651
ggplot2::theme_minimal() +
4752
ggplot2::theme(

man/get_scenario.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/plot_scenario.Rd

Lines changed: 5 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/plot_sealevel.Rd

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/figs.Rmd

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ knitr::opts_chunk$set(
2323

2424
```{r setup}
2525
library(slrcsap)
26+
library(ggplot2)
2627
```
2728

2829
This vignette provides two figures that support the 2025 update of sea level rise projections for the Tampa Bay region. The report was created by the Climate Science Advisory Panel (CSAP) to update recommendations from @tbep0519.
@@ -31,13 +32,18 @@ The first figure shows mean monthly sea level at the St. Petersburg, FL tide gau
3132

3233
```{r sealevelplot, fig.width = 8, fig.height = 3, fig.cap="1947-2025 monthly mean sea level (MSL) in St. Petersburg, FL, NOAA tide gauge 8726520. Seasonal cycle removed."}
3334
dat <- get_sealevel()
34-
plot_sealevel(dat)
35+
plot_sealevel(dat, yrng = c(-1, 1), ybrk = 5)
3536
```
3637

3738
The second figure shows three sea level rise scenarios for St. Petersburg, FL based on regionally corrected NOAA 2022 curves. Download the figure [here](https://tbep-tech.github.io/slrcsap/articles/figs_files/figure-html/scenarioplot.png).
3839

3940
```{r scenarioplot, fig.width = 8, fig.height = 4, fig.cap="Graphic relative sea level change (RSLC) scenarios for St. Petersburg, FL, as calculated using the regionally corrected NOAA 2022 curves."}
4041
dat <- get_scenario()
41-
plot_scenario(dat)
42+
plot_scenario(dat, yrng = c(0, 6)) +
43+
labs(
44+
title = 'Relative Sea Level Change Predictions',
45+
subtitle = 'Gauge 8726520, St. Petersburg, FL'
46+
)
4247
```
4348

49+
## References

vignettes/refs.bib

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
@techreport{sweet2022,
2+
title={Global and Regional Sea Level Rise Scenarios for the {U}nited {S}tates: Updated Mean Projections and Extreme Water Level Probabilities Along {U.S.} Coastlines},
3+
author={Sweet, W.V. and Hamlington, B.D. and Kopp, R.E. and Weaver, C.P. and Barnard, P.L. and Bekaert, D. and Brooks, W. and Craghan, M. and Dusek, G. and Frederikse, T. and Garner, G. and Genz, A.S. and Krasting, J.P. and Larour, E. and Marcy, D. and Marra, J.J. and Obeysekera, J. and Osler, M. and Pendleton, M. and Roman, D. and Schmied, L. and Veatch, W. and White, K.D. and Zuzak, C.},
4+
year={2022},
5+
institution={National Oceanic and Atmospheric Administration, National Ocean Service},
6+
type={NOAA Technical Report},
7+
number={NOS 01},
8+
address={Silver Spring, MD},
9+
pages={111},
10+
url={https://oceanservice.noaa.gov/hazards/sealevelrise/noaa-nostechrpt01-global-regional-SLR-scenarios-US.pdf}
11+
}
12+
113
@techreport{tbep0519,
214
author={M. Burke and L. Carnahan and K. Hammer-Levy and G. Mitchum},
315
year={2019},

vignettes/slrcsap.Rmd

Lines changed: 92 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,99 @@ library(slrcsap)
3636

3737
The package includes two core functions to download data and two core functions to plot data. The two download functions are `get_sealevel()` and `get_scenario()` to download tidal gauge data and sea level rise scenario data, respectively. The corresponding plot functions to view data retrieved from the download functions are `plot_sealevel()` and `plot_scenario()`. The content below demonstrates how to use each function, including the optional arguments that can be used to customize the output.
3838

39-
Tidal gauge data is downloaded from the NOAA Tides and Currents website. The data is available for all NOAA tide gauges, but the package is currently set up to download data for the St. Petersburg, FL gauge (NOAA ID 8726520). The data is downloaded as a CSV file and then read into R. The data includes monthly mean sea level (MSL) values from 1947 to the present, including a seasonal correction.
39+
### Sea level Data
40+
41+
Sea level data is downloaded from the [NOAA Tides and Currents](https://tidesandcurrents.noaa.gov){target="_blank} website. The data is available for all NOAA tide gauges and is setup to download data for the St. Petersburg, FL gauge (NOAA ID 8726520) by default. The data is read directly into R from the URL <https://tidesandcurrents.noaa.gov/sltrends/data/8726520_meantrend.txt>. The data includes monthly mean sea level (MSL) values from 1947 to the present, including a seasonal correction.
42+
43+
```{r}
44+
# Download sea level data for St. Petersburg
45+
spsealevel <- get_sealevel()
46+
head(spsealevel)
47+
```
48+
49+
Data for alternative stations can be obtained using the `gauge` argument.
50+
51+
```{r}
52+
# Download sea level data for Cedar Key
53+
cksealevel <- get_sealevel(gauge = 8727520)
54+
head(cksealevel)
55+
```
56+
57+
The sea level data can be plotted using the `plot_sealevel()` function.
58+
59+
```{r, figheight = 3, fig.width = 8}
60+
# Plot sea level data for St. Petersburg
61+
plot_sealevel(spsealevel)
62+
63+
# Plot sea level data for Cedar Key
64+
plot_sealevel(cksealevel)
65+
```
66+
67+
Various arguments for `plot_sealevel()` can change the appearance of the plot. Below, the color, units, and x-axis range are modified
68+
69+
```{r, fig.height = 3, fig.width = 8}
70+
# Change arguments for the plot
71+
plot_sealevel(spsealevel, col = 'tomato1', units = 'm',
72+
xrng = as.Date(c('2000-01-01', '2023-01-01')))
73+
```
74+
75+
The plot is also a `ggplot()` object and can be modified with additional [ggplot2](https://ggplot2.tidyverse.org/){target="_blank"} functions. Below, the plot is modified to add a title and change the theme.
76+
77+
```{r, fig.height = 3, fig.width = 8}
78+
# Add a title and change the theme
79+
library(ggplot2)
80+
plot_sealevel(spsealevel) +
81+
ggtitle('Monthly Mean Sea Level (MSL) at St. Petersburg, FL') +
82+
theme_grey()
83+
```
84+
85+
### Sea Level Rise Scenarios
86+
87+
Sea level rise scenarios can be downloaded using the `get_scenario()` function. Data are downloaded from the [Interagency Sea Level Rise Scenario Tool](https://sealevel.nasa.gov/task-force-scenario-tool){target="_blank} website. Details of the methods used in this tool are found in the technical report [@sweet2022]. The data are downloaded as an Excel sheet to from the URL <https://sealevel.nasa.gov/task-force-scenario-tool?psmsl_id=520>, set to St. Petersburg, FL by default using regionally corrected NOAA 2022 curves. Emissions scenarios of NOAA Intermediate Low, Intermediate, and Intermediate High are downloaded by default, as recommended by the Climate Science Advisory Panel. The data show relative sea level change (RSLC) from 2020 to 2100 for each scenario in meters and feet.
4088

4189
```{r}
42-
# Download tidal gauge data
43-
dat <- get_sealevel()
44-
head(dat)
90+
# Download sea level rise scenarios for St. Petersburg
91+
spscenario <- get_scenario()
92+
head(spscenario)
93+
```
94+
95+
Data for alternative locations and scenarios can be obtained using the `id` and `scenario` arguments, respectively.
96+
97+
```{r}
98+
# Download sea level rise scenarios for Cedar Key
99+
ckscenario <- get_scenario(id = 428, scenario = c('Low', 'IntLow', 'Int', 'IntHigh', 'High'))
100+
head(ckscenario)
101+
```
102+
103+
The sea level rise scenarios can be plotted using the `plot_scenario()` function.
104+
105+
```{r, fig.height = 4, fig.width = 8}
106+
# Plot sea level rise scenarios for St. Petersburg
107+
plot_scenario(spscenario)
108+
109+
# Plot sea level rise scenarios for Cedar Key
110+
plot_scenario(ckscenario)
111+
```
112+
113+
Various arguments for `plot_sealevel()` can change the appearance of the plot. Below, the color ramp, units, and x-axis range are modified
114+
115+
```{r, fig.height = 4, fig.width = 8}
116+
# Change arguments for the plot
117+
plot_scenario(spscenario, cols = c('green', 'blue', 'red'), units = 'm',
118+
xrng = c(2020, 2150))
119+
```
120+
121+
The plot is also a `ggplot` object and can be modified with additional [ggplot2](https://ggplot2.tidyverse.org/){target="_blank"} functions. Below, the plot is modified to add a title, subtitle, and change the theme.
122+
123+
```{r, fig.height = 4, fig.width = 8}
124+
# Add a title, subtitle and change the theme
125+
plot_scenario(spscenario) +
126+
labs(
127+
title = 'Relative Sea Level Change Predictions',
128+
subtitle = 'Gauge 8726520, St. Petersburg, FL'
129+
) +
130+
theme_grey() +
131+
theme(legend.position = 'bottom')
45132
```
46133

134+
## References

0 commit comments

Comments
 (0)