Skip to content

Commit

Permalink
enh(plot_idf): labels
Browse files Browse the repository at this point in the history
fixes #56
  • Loading branch information
dimfalk committed Mar 3, 2024
1 parent 7844d6a commit 0b13e1f
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 8 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: kostra2010R
Title: R Interface for KOSTRA-DWD-2010r Dataset
Version: 0.11.1
Version: 0.11.2
Date: 2024-03-03
Authors@R:
person("Dimitri", "Falk", , "[email protected]", role = c("aut", "cre"))
Expand Down
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# version 0.11.0
# version 0.11.2

## features

Expand Down
38 changes: 36 additions & 2 deletions R/plot_idf.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#' @param x Tibble containing grid cell statistics from KOSTRA-DWD-2010R,
#' as provided by `get_stats()`.
#' @param tn (optional) numeric. Return period \code{[a]} to be used for filtering.
#' @param log10 logical. Transform x axis to log10 scale?
#'
#' @return ggplot object.
#' @export
Expand All @@ -13,8 +14,11 @@
#' get_stats("49011") |> plot_idf()
#'
#' get_stats("49011") |> plot_idf(tn = 100)
#'
#' get_stats("49011") |> plot_idf(log10 = TRUE)
plot_idf <- function(x = NULL,
tn = NULL) {
tn = NULL,
log10 = FALSE) {

# debugging ------------------------------------------------------------------

Expand All @@ -23,6 +27,8 @@ plot_idf <- function(x = NULL,

# tn <- 100

# log10 <- TRUE

# check arguments ------------------------------------------------------------

checkmate::assert_tibble(x)
Expand All @@ -36,6 +42,8 @@ plot_idf <- function(x = NULL,
checkmate::test_choice(tn, allowed_tn)
)

checkmate::assert_logical(log10, len = 1)

# pre-processing -------------------------------------------------------------

# optionally filter for tn
Expand All @@ -50,6 +58,7 @@ plot_idf <- function(x = NULL,
attr(x, "returnperiods_a") <- tn
}

# labels
if (attr(x, "type") == "HN") {

cnames <- colnames(x)[colnames(x) |> stringr::str_detect("HN_*")]
Expand Down Expand Up @@ -82,10 +91,23 @@ plot_idf <- function(x = NULL,

lab_legend <- attr(x, "returnperiods_a") |> as.character()


# y-axis ticks
val_max <- x_long[["value"]] |> max() |> round(-1)

if(val_max < 200) {

y_ticks <- seq(from = 0, to = val_max, by = 10)

} else {

y_ticks <- seq(from = 0, to = val_max, by = 20)
}

# main -----------------------------------------------------------------------

# plot tile statistics, colours according to return periods
ggplot2::ggplot(x_long, ggplot2::aes(x = D_min, y = value, colour = name)) +
gg <- ggplot2::ggplot(x_long, ggplot2::aes(x = D_min, y = value, colour = name)) +
ggplot2::geom_point() +
ggplot2::geom_line() +
ggplot2::labs(title = lab_title,
Expand All @@ -94,5 +116,17 @@ plot_idf <- function(x = NULL,
x = "duration [min]",
y = lab_y,
color = "return periods [a]") +
ggplot2::scale_y_continuous(breaks = y_ticks,
labels = y_ticks) +
ggplot2::scale_color_discrete(labels = lab_legend)

if (log10 == TRUE) {

gg <- gg + ggplot2::scale_x_continuous(trans = "log10",
breaks = attr(x, "durations_min"),
labels = attr(x, "durations_min"))
}

# return object
gg
}
4 changes: 2 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ xts

Data can additionally be visualized as intensity-duration-frequency curves using `plot_idf()`, underpinned by `{ggplot2}` ...

```{r}
plot_idf(kostra)
```{r, fig.width = 10}
plot_idf(kostra, log10 = TRUE)
```

... or exported to disk using `write_stats()` based on `write.table()`.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ and load the package via

``` r
library(kostra2010R)
#> 0.11.0
#> 0.11.2
```

## Getting started
Expand Down Expand Up @@ -367,7 +367,7 @@ Data can additionally be visualized as intensity-duration-frequency
curves using `plot_idf()`, underpinned by `{ggplot2}`

``` r
plot_idf(kostra)
plot_idf(kostra, log10 = TRUE)
```

<img src="man/figures/README-unnamed-chunk-21-1.png" width="100%" />
Expand Down
Binary file modified man/figures/README-unnamed-chunk-21-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions tests/testthat/test-plot_idf.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ test_that("Output class is as expected.", {
gg3 <- get_stats("49011") |> plot_idf(tn = 100)

expect_s3_class(gg3, c("gg", "ggplot"))

gg4 <- get_stats("49011") |> plot_idf(log10 = TRUE)

expect_s3_class(gg4, c("gg", "ggplot"))
})

0 comments on commit 0b13e1f

Please sign in to comment.