R语言 在保留标准模板的同时删除绘图悬停中的轨迹名称

pn9klfpd  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(118)

我生成了一个plotly瀑布图,如下所示:

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))

由于存在第二个text跟踪,默认情况下跟踪名称将添加到悬停文本中:

我知道我可以通过提供一个自定义的hovertemplate来删除它,我在其中添加了一个空的<extra></extra>标记。我的问题是我很喜欢默认的hovertemplate,我不知道如何复制它。
因此,我的问题是,我必须提供哪个hovertemplate字符串才能像默认情况下一样获得确切的悬停,但不需要跟踪名称框?
这是我尝试过的,但它没有给予我的漂亮三角形,并显示了太多的文本为非relative酒吧?

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)
v2g6jxz6

v2g6jxz61#

由于您不需要图例,您可以在add_trace(type = "waterfall", ......)中添加name = ""

相关问题