Skip to content

Commit 17901ca

Browse files
committed
don't remove positional missing values in waterfall charts, closes #1517
1 parent 4763648 commit 17901ca

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

Diff for: R/plotly_build.R

+3-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,9 @@ plotly_build.plotly <- function(p, registerFrames = TRUE) {
226226
# 3. The grouping from (2) and any groups detected via dplyr::groups()
227227
# are combined into a single grouping variable, .plotlyGroupIndex
228228
builtData <- arrange_safe(builtData, ".plotlyTraceIndex")
229-
isComplete <- complete.cases(builtData[names(builtData) %in% c("x", "y", "z")])
229+
# Missing values have special meaning for waterfall
230+
vars <- if (trace$type != "waterfall") c("x", "y", "z")
231+
isComplete <- complete.cases(builtData[names(builtData) %in% vars])
230232
# warn about missing values if groups aren't relevant for this trace type
231233
if (any(!isComplete) && !has_group(trace)) {
232234
warning("Ignoring ", sum(!isComplete), " observations", call. = FALSE)

Diff for: tests/testthat/test-plotly-waterfall.R

+16
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,20 @@ test_that("Simple waterfall works", {
1111
expect_doppelganger_built(p, "waterfall-simple")
1212
})
1313

14+
test_that("Waterfall missing values are retained", {
15+
y <- c("Sales", "Consulting", "Maintenance", "Other revenue", "Net revenue", "Purchases", "Material expenses", "Personnel expenses", "Other expenses", "Operating profit", "Investment income", "Financial income", "Profit before tax", "Income tax (15%)", "Profit after tax")
16+
17+
d <- data.frame(
18+
measure = c("relative", "relative", "relative", "relative", "total", "relative", "relative", "relative", "relative", "total", "relative", "relative", "total", "relative", "total"),
19+
y = factor(y, levels = y),
20+
x = c(375, 128, 78, 27, NA, -327, -12, -78, -12, NA, 32, 89, NA, -45, NA)
21+
)
22+
23+
p <- plot_ly(d, measure = ~measure, y = ~y, x = ~x) %>%
24+
add_trace(type = "waterfall", orientation = "h") %>%
25+
layout(yaxis = list(autorange = "reversed"))
26+
27+
expect_doppelganger_built(p, "waterfall-missing-values")
28+
})
29+
1430

0 commit comments

Comments
 (0)