Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion R/layers2traces.R
Original file line number Diff line number Diff line change
Expand Up @@ -1081,7 +1081,46 @@ make_error <- function(data, params, xy = "x") {

# function to transform geom_ribbon data into format plotly likes
# (note this function is also used for geom_smooth)
ribbon_dat <- function(dat) {
ribbon_dat <- function(dat, connectgaps = FALSE) {
# Let Plotly.js bridge gaps along each edge of a single polygon.
if (isTRUE(connectgaps)) return(ribbon_polygon(dat))

missing <- is.na(dat[["ymin"]]) | is.na(dat[["ymax"]])
if (!any(missing)) return(ribbon_polygon(dat))

if (".plotlyGroupIndex" %in% names(dat)) {
group <- ".plotlyGroupIndex"
series <- c(".plotlyTraceIndex", group)
} else {
group <- "group"
series <- c("PANEL", "frame", group)
}
series <- intersect(series, names(dat))
order_by <- dat[c(series, "x")]
if (".plotlyTraceIndex" %in% series) {
# traceify() draws character traces in their first appearance in x order.
trace <- dat[[".plotlyTraceIndex"]]
order_by[[".plotlyTraceIndex"]] <- match(trace, trace[order(dat[["x"]])])
}
rows <- do.call(order, order_by)
dat <- dat[rows, , drop = FALSE]
missing <- missing[rows]

# Give each complete run its own group so group2NA() closes it at the gap.
series_id <- vctrs::vec_group_id(dat[series])
first <- !duplicated(series_id)
dat[[group]] <- cumsum(missing | first)

# Keep a blank row for an empty series so an animation can clear its trace.
empty <- first & !series_id %in% series_id[!missing]
dat[["ymin"]][empty] <- NA
dat[["ymax"]][empty] <- NA
dat <- dat[!missing | empty, , drop = FALSE]
polygons <- lapply(split(dat, dat[[group]]), ribbon_polygon)
structure(do.call(rbind, polygons), class = oldClass(dat))
}

ribbon_polygon <- function(dat) {
n <- nrow(dat)
if (n == 0) return(dat)
o <- order(dat[["x"]])
Expand Down
2 changes: 1 addition & 1 deletion R/plotly_build.R
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ registerFrames <- function(p, frameMapping = NULL) {

train_data <- function(data, trace) {
if (inherits(trace, "plotly_ribbon")) {
data <- ribbon_dat(data)
data <- ribbon_dat(data, connectgaps = trace[["connectgaps"]])
}
if (inherits(trace, "plotly_segment")) {
# TODO: this could be faster, more efficient
Expand Down
32 changes: 32 additions & 0 deletions tests/testthat/test-ggplot-ribbon.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,35 @@ test_that("geom_ribbon() with fill aesthetic produces multiple traces", {
# 10 traces -- one for each decade
info <- expect_traces(p4, 10, "fill")
})

test_that("geom_ribbon() starts a new polygon after missing bounds", {
df <- data.frame(
x = 1:6,
ymin = c(1, 2, NA, 4, 5, 6),
ymax = c(11, 12, 13, NA, 15, 16)
)
p <- ggplot(df, aes(x, ymin = ymin, ymax = ymax)) + geom_ribbon()
trace <- plotly_build(p)$x$data[[1]]

expect_equal(as.numeric(trace$x), c(1, 2, 2, 2, 1, 1, NA, 5, 6, 6, 6, 5, 5))
expect_equal(as.numeric(trace$y), c(1, 2, 2, 12, 11, 1, NA, 5, 6, 6, 16, 15, 5))
})

test_that("geom_ribbon() clears the ribbon in a frame with missing bounds", {
df <- data.frame(
x = rep(1:3, 2),
ymin = c(1:3, rep(NA_real_, 3)),
ymax = c(4:6, rep(NA_real_, 3)),
stage = rep(c("complete", "missing"), each = 3)
)
p <- ggplot(df, aes(x, ymin = ymin, ymax = ymax, frame = stage)) +
geom_ribbon()
frames <- as.list(plotly_build(p)$x$frames)
names(frames) <- vapply(frames, function(frame) frame$name, character(1))

expect_identical(names(frames), c("complete", "missing"))
expect_true(any(!is.na(frames[["complete"]]$data[[1]][["y"]])))
expect_identical(as.integer(frames[["missing"]]$traces), 0L)
y <- frames[["missing"]]$data[[1]][["y"]]
expect_true(length(y) > 0L && all(is.na(y)))
})
74 changes: 74 additions & 0 deletions tests/testthat/test-plotly-ribbon.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
test_that("add_ribbons() splits missing bounds within each group", {
df <- data.frame(
group = c(rep("a", 6), rep("b", 2)),
x = c(1:6, 3:4),
ymin = c(1, 2, NA, 4, 5, 6, 0, 0),
ymax = c(11, 12, 13, NA, 15, 16, 1, 1)
)
df <- dplyr::group_by(df[c(5, 4, 1, 7, 6, 2, 3, 8), ], group)
p <- add_ribbons(plot_ly(df), x = ~x, ymin = ~ymin, ymax = ~ymax)
trace <- plotly_build(p)$x$data[[1]]

expect_equal(trace$fill, "toself")
expect_equal(
as.numeric(trace$x),
c(1, 2, 2, 2, 1, NA, 5, 6, 6, 6, 5, NA, 3, 4, 4, 4, 3)
)
expect_equal(
as.numeric(trace$y),
c(1, 2, 2, 12, 11, NA, 5, 6, 6, 16, 15, NA, 0, 0, 0, 1, 1)
)
})

test_that("add_ribbons() connects gaps along each ribbon edge", {
df <- data.frame(
x = 1:5,
ymin = c(0, 0, NA, 0, 0),
ymax = c(1, 1, NA, 1, 1)
)
p <- add_ribbons(plot_ly(df), x = ~x, ymin = ~ymin, ymax = ~ymax,
connectgaps = TRUE)
trace <- plotly_build(p)$x$data[[1]]
valid <- !is.na(trace$y)

expect_true(trace$connectgaps)
expect_equal(as.numeric(trace$x[valid]), c(1, 2, 4, 5, 5, 5, 4, 2, 1))
expect_equal(as.numeric(trace$y[valid]), c(0, 0, 0, 0, 0, 1, 1, 1, 1))
})

test_that("add_ribbons() clears the ribbon in a frame with missing bounds", {
df <- data.frame(
x = rep(1:3, 2),
ymin = c(1:3, rep(NA_real_, 3)),
ymax = c(4:6, rep(NA_real_, 3)),
stage = rep(c("complete", "missing"), each = 3)
)
p <- add_ribbons(plot_ly(df), x = ~x, ymin = ~ymin, ymax = ~ymax,
frame = ~stage)
frames <- as.list(plotly_build(p)$x$frames)
names(frames) <- vapply(frames, function(frame) frame$name, character(1))

expect_identical(names(frames), c("complete", "missing"))
expect_true(any(!is.na(frames[["complete"]]$data[[1]][["y"]])))
expect_identical(as.integer(frames[["missing"]]$traces), 0L)
y <- frames[["missing"]]$data[[1]][["y"]]
expect_true(length(y) > 0L && all(is.na(y)))
})

test_that("add_ribbons() keeps the trace order when a bound is missing", {
df <- data.frame(
x = c(2:5, 1:4),
ymin = 1:8,
ymax = 11:18,
series = rep(c("a", "b"), each = 4)
)
trace_names <- function(data) {
p <- add_ribbons(plot_ly(data), x = ~x, ymin = ~ymin, ymax = ~ymax,
color = ~series)
vapply(plotly_build(p)$x$data, function(trace) trace$name, character(1))
}

expect_identical(trace_names(df), c("b", "a"))
df$ymin[3] <- NA_integer_
expect_identical(trace_names(df), c("b", "a"))
})