diff --git a/R/plotly_build.R b/R/plotly_build.R
index 2cf5de65aa..487fabded0 100644
--- a/R/plotly_build.R
+++ b/R/plotly_build.R
@@ -226,7 +226,9 @@ plotly_build.plotly <- function(p, registerFrames = TRUE) {
# 3. The grouping from (2) and any groups detected via dplyr::groups()
# are combined into a single grouping variable, .plotlyGroupIndex
builtData <- arrange_safe(builtData, ".plotlyTraceIndex")
- isComplete <- complete.cases(builtData[names(builtData) %in% c("x", "y", "z")])
+ # Missing values have special meaning for waterfall
+ vars <- if (trace$type != "waterfall") c("x", "y", "z")
+ isComplete <- complete.cases(builtData[names(builtData) %in% vars])
# warn about missing values if groups aren't relevant for this trace type
if (any(!isComplete) && !has_group(trace)) {
warning("Ignoring ", sum(!isComplete), " observations", call. = FALSE)
diff --git a/tests/figs/subplot/plotly-subplot-geo-cartesian.svg b/tests/figs/subplot/plotly-subplot-geo-cartesian.svg
index f60e007dba..514cecd0fa 100644
--- a/tests/figs/subplot/plotly-subplot-geo-cartesian.svg
+++ b/tests/figs/subplot/plotly-subplot-geo-cartesian.svg
@@ -1 +1 @@
-
+
diff --git a/tests/figs/waterfall/waterfall-missing-values.svg b/tests/figs/waterfall/waterfall-missing-values.svg
new file mode 100644
index 0000000000..70ddc7d1b4
--- /dev/null
+++ b/tests/figs/waterfall/waterfall-missing-values.svg
@@ -0,0 +1 @@
+
diff --git a/tests/testthat/test-plotly-waterfall.R b/tests/testthat/test-plotly-waterfall.R
index 3edd4ed044..9556c699aa 100644
--- a/tests/testthat/test-plotly-waterfall.R
+++ b/tests/testthat/test-plotly-waterfall.R
@@ -11,4 +11,20 @@ test_that("Simple waterfall works", {
expect_doppelganger_built(p, "waterfall-simple")
})
+test_that("Waterfall missing values are retained", {
+ 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")
+
+ d <- data.frame(
+ measure = c("relative", "relative", "relative", "relative", "total", "relative", "relative", "relative", "relative", "total", "relative", "relative", "total", "relative", "total"),
+ y = factor(y, levels = y),
+ x = c(375, 128, 78, 27, NA, -327, -12, -78, -12, NA, 32, 89, NA, -45, NA)
+ )
+
+ p <- plot_ly(d, measure = ~measure, y = ~y, x = ~x) %>%
+ add_trace(type = "waterfall", orientation = "h") %>%
+ layout(yaxis = list(autorange = "reversed"))
+
+ expect_doppelganger_built(p, "waterfall-missing-values")
+})
+