Skip to content

Commit

Permalink
Merge pull request #194 from mcol/issue_189
Browse files Browse the repository at this point in the history
Support vectors and single-column data frames in plot_KDE()
  • Loading branch information
RLumSK authored Sep 3, 2024
2 parents e6f875c + 1d6a51f commit 3064e64
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 23 deletions.
5 changes: 5 additions & 0 deletions NEWS.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ which would generate unexpected warnings (#163, @mcol).
* The function now calculates the relative saturation (`n/N`) using the ration of the two integrates.
The values is part of the output table.

### `plot_KDE()`
* It now officially supports numeric vectors and single-column data frames,
for which it assumes that the De error at each measurement is 10^-9 (#189,
fixed in #194, @mcol).

### `plot_NRt()`
* The function reports an helpful message rather than crashing when applied
to an object of unexpected type or when there is a mismatch in time values
Expand Down
51 changes: 30 additions & 21 deletions R/plot_KDE.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@
#' with the appropriate keyword using the argument `summary.method`.
#'
#'
#' @param data [data.frame] or [RLum.Results-class] object (**required**):
#' for `data.frame`: two columns: De (`values[,1]`) and De error (`values[,2]`).
#' @param data [data.frame], [vector] or [RLum.Results-class] object (**required**):
#' for `data.frame`: either two columns: De (`values[,1]`) and De error
#' (`values[,2]`), or one: De (`values[,1]`). If a numeric vector or a
#' single-column data frame is provided, De error is assumed to be 10^-9
#' for all measurements and error bars are not drawn.
#' For plotting multiple data sets, these must be provided as
#' `list` (e.g. `list(dataset1, dataset2)`).
#'
Expand Down Expand Up @@ -222,12 +225,16 @@ plot_KDE <- function(
data[[i]] <- get_RLum(data[[i]], "data")[,1:2]
}

##make sure we only take the first two columns
data[[i]] <- data[[i]][,1:2]

##account for very short datasets
if(length(data[[i]]) < 2) {
data[[i]] <- cbind(data[[i]], rep(NA, length(data[[i]])))
## if `data[[i]]` is a numeric vector or a single-column data frame,
## append a second column with a small non-zero value (10^-9 for
## consistency with what `calc_Statistics() does)
if (NCOL(data[[i]]) < 2) {
data[[i]] <- data.frame(data[[i]], 10^-9)
attr(data[[i]], "De.errors.available") <- FALSE
} else {
## keep only the first two columns
data[[i]] <- data[[i]][, 1:2]
attr(data[[i]], "De.errors.available") <- TRUE
}
}

Expand Down Expand Up @@ -280,13 +287,16 @@ plot_KDE <- function(
## optionally, count and exclude NA values and print result
if(na.rm == TRUE) {
for(i in 1:length(data)) {
n.NA <- sum(is.na(data[[i]][,1]))
na.idx <- which(is.na(data[[i]][, 1]))
n.NA <- length(na.idx)
if(n.NA == 1) {
message(paste("1 NA value excluded from data set", i, "."))
} else if(n.NA > 1) {
message(paste(n.NA, "NA values excluded from data set", i, "."))
}
data[[i]] <- na.exclude(data[[i]])
if (n.NA > 0) {
data[[i]] <- data[[i]][-na.idx, ]
}
}
}

Expand Down Expand Up @@ -368,8 +378,6 @@ plot_KDE <- function(
De.density.range[i,1:4] <- NA
De.stats[i,4] <- NA
}


}

## Get global range of densities
Expand Down Expand Up @@ -1210,15 +1218,16 @@ plot_KDE <- function(

## add De error bars
for(i in 1:length(data)) {
arrows(data[[i]][,1] - data[[i]][,2],
1:length(data[[i]][,1]),
data[[i]][,1] + data[[i]][,2],
1:length(data[[i]][,1]),
code = 3,
angle = 90,
length = 0.05,
col = col.value.bar[i])

if (attr(data[[i]], "De.errors.available")) {
arrows(data[[i]][, 1] - data[[i]][, 2],
1:length(data[[i]][,1]),
data[[i]][, 1] + data[[i]][, 2],
1:length(data[[i]][, 1]),
code = 3,
angle = 90,
length = 0.05,
col = col.value.bar[i])
}
## add De measurements
points(data[[i]][,1], 1:De.stats[i,1],
col = col.value.dot[i],
Expand Down
7 changes: 5 additions & 2 deletions man/plot_KDE.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tests/testthat/test_plot_KDE.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ test_that("check functionality", {
expect_silent(plot_KDE(data = df, summary.pos = "bottom"))
expect_silent(plot_KDE(data = df, summary.pos = "bottomright"))

## numeric vector
expect_silent(plot_KDE(df[, 1]))

## single-column data.frame
expect_silent(plot_KDE(df[, 1, drop = FALSE]))

## RLum.Results object
expect_silent(plot_KDE(calc_CommonDose(df, plot = FALSE, verbose = FALSE)))

Expand Down

0 comments on commit 3064e64

Please sign in to comment.