Skip to content

Commit

Permalink
fix plot_twilight() for twl_offset
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafnuss committed Jun 30, 2024
1 parent 4ba80a5 commit 2734537
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
17 changes: 10 additions & 7 deletions R/plot.tag.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand All @@ -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"
Expand Down Expand Up @@ -520,8 +525,6 @@ plot_tag_twilight <- function(tag,
shape = 16
)
}


}
p <- p +
ggplot2::geom_point(
Expand Down
8 changes: 6 additions & 2 deletions tests/testthat/test-geolight.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})


Expand Down

0 comments on commit 2734537

Please # to comment.