diff --git a/R/layers2traces.R b/R/layers2traces.R index 181ef72909..cb6d3fac8b 100644 --- a/R/layers2traces.R +++ b/R/layers2traces.R @@ -713,6 +713,10 @@ geom2trace.GeomBlank <- function(data, params, p) { #' @export geom2trace.GeomPath <- function(data, params, p) { data <- group2NA(data) + direction <- if (inherits(data, "GeomStep")) params$direction %||% "hv" + if (!is.null(direction) && direction %in% c("hv", "vh")) { + data <- step_gap_corners(data, direction) + } L <- list( x = data[["x"]], y = data[["y"]], @@ -735,10 +739,40 @@ geom2trace.GeomPath <- function(data, params, p) { ), hoveron = hover_on(data) ) - if (inherits(data, "GeomStep")) L$line$shape <- params$direction %||% "hv" + if (!is.null(direction)) L$line$shape <- direction + if (!is.null(data$.plotlyStepCorner)) { + L$hoverinfo <- ifelse(data$.plotlyStepCorner, "skip", "text") + } compact(L) } +step_gap_corners <- function(data, direction) { + missing_y <- !is.na(data$x) & is.na(data$y) + if (!any(missing_y)) return(data) + + known <- !is.na(data$x) & !is.na(data$y) + # ggplot2 trims missing values at each group's ends before drawing steps. + interior <- stats::ave(known, data$group, FUN = function(points) { + dplyr::cumany(points) & rev(dplyr::cumany(rev(points))) + }) + offset <- if (identical(direction, "hv")) -1L else 1L + gap <- which(missing_y & interior) + gap <- gap[known[gap + offset]] + if (!length(gap)) return(data) + + # Preserve the horizontal segment at each gap without making a hover point. + data$.plotlyStepCorner <- FALSE + corners <- data[gap + offset, , drop = FALSE] + corners$x <- data$x[gap] + corners$.plotlyStepCorner <- TRUE + payload <- intersect(c("hovertext", "key", "ids", "customdata"), names(corners)) + corners[payload] <- data[rep(NA_integer_, length(gap)), payload, drop = FALSE] + + corner_positions <- gap + offset / 2 + rows <- order(c(seq_len(nrow(data)), corner_positions)) + rbind(data, corners)[rows, , drop = FALSE] +} + #' @export geom2trace.GeomPoint <- function(data, params, p) { shape <- aes2plotly(data, params, "shape") diff --git a/tests/testthat/test-ggplot-step.R b/tests/testthat/test-ggplot-step.R index 500b30c1a0..0fe172ea9f 100644 --- a/tests/testthat/test-ggplot-step.R +++ b/tests/testthat/test-ggplot-step.R @@ -48,3 +48,37 @@ test_that("`stat_ecdf` renders correctly", { p <- ggplot(df, aes(x, colour = g)) + stat_ecdf() expect_doppelganger(ggplotly(p), "step-ecdf-multiple") }) + +test_that("step traces reach the edge of a missing observation", { + d <- data.frame(x = 1:5, y = c(1, 2, NA, 1, 1)) + p <- ggplot(d, aes(x, y)) + + hv <- plotly_build(ggplotly(p + geom_step()))$x$data[[1]] + expect_identical(hv$line$shape, "hv") + expect_equal(as.numeric(hv$x), c(1, 2, 3, 3, 4, 5)) + expect_equal(as.numeric(hv$y), c(1, 2, 2, NA, 1, 1)) + expect_identical(as.character(hv$hoverinfo), c("text", "text", "skip", rep("text", 3))) + + vh <- plotly_build(ggplotly(p + geom_step(direction = "vh")))$x$data[[1]] + expect_identical(vh$line$shape, "vh") + expect_equal(vh$x, hv$x) + expect_equal(as.numeric(vh$y), c(1, 2, NA, 1, 1, 1)) + expect_identical(as.character(vh$hoverinfo), c(rep("text", 3), "skip", rep("text", 2))) +}) + +test_that("step corners at missing values stay within their groups", { + d <- data.frame( + x = c(1:7, 1:4), + y = c(NA, 2, NA, NA, 3, 4, NA, 8, NA, 9, 10), + group = rep(c("a", "b"), c(7, 4)), + id = letters[1:11] + ) + p <- ggplot(d, aes(x, y, group = group, key = id)) + geom_step() + trace <- plotly_build(ggplotly(p))$x$data[[1]] + corners <- which(trace$hoverinfo == "skip") + + expect_equal(trace$x[corners], c(3, 2)) + expect_equal(trace$y[corners], c(2, 8)) + expect_true(all(is.na(trace$key[corners]))) + expect_length(trace$key, length(trace$x)) +})