Home > OS >  Remove Trace Name in Plotly Hover while Keeping Standard Template
Remove Trace Name in Plotly Hover while Keeping Standard Template

Time:11-29

I produce a plotly waterfall chart like this:

library(plotly)
library(dplyr)

data <- tibble(x = c("Sales", "Consulting", "Net revenue", "New Intermediate",
                     "Purchases", "Other expenses", "Profit before tax"), 
               measure = c("relative", "relative", "total", "absolute", "relative", 
                           "relative", "total"),
               text = c(" 60", " 80", "", "", "-40", 
                        "-20", "Total"),
               y = c(60, 80, 0, 120, -40, -20, 0)) %>% 
  mutate(x = factor(x, x))


(fig <- data %>% 
    plot_ly() %>% 
    add_text(x = factor("Sales", levels = data$x),
           y = 80, text = "Some Text") %>%
    add_trace(type = "waterfall", measure = ~measure,
              x = ~x, textposition = "outside", y= ~y, text =~text,
              connector = list(line = list(color= "rgb(63, 63, 63)"))) %>%
    layout(title = "Profit and loss statement 2018",
           xaxis = list(title = ""),
           yaxis = list(title = ""),
           autosize = TRUE,
           showlegend = FALSE))

Because of the second text trace, the trace name is added by default to the hovertext:

Hover Text shows Trace Name

I know that I can remove it by supplying a custom hovertemplate where I add an empty <extra></extra> tag. My problem is that I quite like the default hovertemplate and I have no clue how I would reproduce it.

Thus, my question is which hovertemplate format string do I have to supply to get the exact hover as in the default case but without the trace name box?

This is what I tried, but it does not give my the nice triangles and shows too much text for non relative bars?

data %>% 
  plot_ly() %>% 
  add_trace(type = "waterfall", measure = ~measure,
            x = ~x, textposition = "outside", y= ~y, text =~text,
            connector = list(line = list(color= "rgb(63, 63, 63)")),
            hovertemplate = "(%{x},%{y})<br>%{text}<br>%{delta}<br>Initial: %{initial}<extra></extra>") %>%
  add_text(x = factor("Sales", levels = data$x),
           y = 80, text = "Some Text") %>% 
  layout(title = "Profit and loss statement 2018",
         xaxis = list(title = ""),
         yaxis = list(title = ""),
         autosize = TRUE,
         showlegend = TRUE)

CodePudding user response:

Since you don't want the legend, you can add name = "" in the add_trace(type = "waterfall", ......).

  • Related