I'm using R/plotly to create a stacked filled area plot summarizing several accounts opened at different times, whose balances sometimes go to zero. Here's a simplified example using three accounts ("X", "Y", and "Z"):
df <- data.frame(cbind(
year <- c("2022-01-01","2022-02-01","2022-03-01","2022-04-01","2022-05-01","2022-06-01","2022-07-01","2022-08-01","2022-09-01","2022-10-01","2022-11-01","2022-12-01"),
x <- c(NA,NA,NA,1,2,3,2,3,4,5,3,4),
y <- c(1,2,2,1,2,4,3,2,NA,NA,NA,NA),
z <- c(NA,NA,NA,1,2,5,6,5,5,NA,NA,NA)
)) %>%
setNames(c("DATE","X","Y","Z")) %>%
mutate(DATE = as.Date(DATE, format = "%Y-%m-%d")) %>%
mutate(X = as.numeric(X), Y = as.numeric(Y), Z = as.numeric(Z)) %>%
pivot_longer(., -DATE, names_to = "LEGEND", values_to = "QUANTITY") %>%
mutate(LEGEND = factor(LEGEND, levels = c("X","Y","Z")))
When I create a graph using plotly, I prefer to use the hovermode = "x"
parameter to compare all quantities at a given time at once. However, when hovering over periods when an account is empty (i.e., QUANTITY = NA), I still get a tooltip box with hoverinfo showing y = 0
. With lots of accounts, this can clutter things up pretty quickly.
dfplot <- plot_ly(data = df, x = ~DATE, y = ~QUANTITY, color = ~LEGEND, type = "scatter", mode = "none", stackgroup = "one", fill = "tonexty") %>%
layout(
autosize = TRUE,
yaxis = list(range = c(0,15)),
hovermode = "x",
showlegend = TRUE
)
dfplot
X and Z are given values of "0", despite being NA
Is there a way to get rid of these extra tooltips? Or to conditionally skip hoverinfo
if y = 0 or NA?
I've been able to skip hoverinfo
for entire traces, and I have been able to get the behavior I want with simple scatter
traces (y = 0 is ignored in these situations). I'm not sure if this is something that is specific to stacked filled area plots or not. Any help would be greatly appreciated!