diff --git a/R/plot.tag.R b/R/plot.tag.R index 72071d34..048e0dbe 100644 --- a/R/plot.tag.R +++ b/R/plot.tag.R @@ -445,18 +445,21 @@ plot_tag_twilight <- function(tag, plot_plotly = FALSE) { tag_assert(tag, "twilight") - l <- tag$light + light <- tag$light if (transform_light) { - l$value <- log(l$value + 0.0001) + abs(min(log(l$value + 0.0001))) + light$value <- log(light$value + 0.0001) + abs(min(log(light$value + 0.0001))) } # Compute the matrix representation of light - mat <- light2mat(l, twl_offset = tag$param$twl_offset) + mat <- light2mat(light, twl_offset = tag$param$twl_offset) # Convert to long format data.fram to be able to plot with ggplot df <- as.data.frame(mat$value) names(df) <- mat$day - df$time <- as.POSIXct(strptime(mat$time, "%H:%M")) # factor(mat$time, levels = mat$time) + time_hour <- as.numeric(substr(mat$time, 1, 2)) + as.numeric(substr(mat$time, 4, 5)) / 60 + time_hour <- time_hour + 24 * (time_hour < tag$param$twl_offset) + df$time <- as.POSIXct(Sys.Date()) + time_hour * 3600 # as.POSIXct(strptime(mat$time, "%H:%M")) # factor(mat$time, levels = mat$time) + df_long <- stats::reshape(df, direction = "long", varying = list(utils::head(names(df), -1)), @@ -477,7 +480,9 @@ plot_tag_twilight <- function(tag, if ("twilight" %in% names(tag)) { twl <- tag$twilight twl$date <- as.Date(twl$twilight) - twl$time <- as.POSIXct(strptime(format(twl$twilight, "%H:%M"), "%H:%M")) + time_hour <- as.numeric(substr(format(twl$twilight, "%H:%M"), 1, 2)) + as.numeric(substr(format(twl$twilight, "%H:%M"), 4, 5)) / 60 + time_hour <- time_hour + 24 * (time_hour < tag$param$twl_offset) + twl$time <- as.POSIXct(Sys.Date()) + time_hour * 3600 if ("label" %in% names(twl)) { twl$discard <- twl$label == "discard" @@ -520,8 +525,6 @@ plot_tag_twilight <- function(tag, shape = 16 ) } - - } p <- p + ggplot2::geom_point( diff --git a/tests/testthat/test-geolight.R b/tests/testthat/test-geolight.R index 375231c1..dacdf12a 100644 --- a/tests/testthat/test-geolight.R +++ b/tests/testthat/test-geolight.R @@ -18,9 +18,13 @@ test_that("Check twilight_create()", { expect_true(nrow(tag$twilight) > 0) expect_no_error(tag_off <- twilight_create(tag, twl_offset = 2)) + expect_equal(tag_off$param$twl_offset, 2) - tag_off_0 <- twilight_create(tag) - expect_equal(tag_off_0$param$twl_offset * 60 * 60, -3300) + expect_no_error(plot_tag_twilight(tag)) + expect_no_error(plot_tag_twilight(tag_off)) + + expect_warning(tag_off <- twilight_create(tag, twl_offset = 3.5)) + plot_tag_twilight(tag_off) })