保留Plotly默认悬停模板 移除悬停中的迹名称
问题
使用R语言的plotly包绘制瀑布图时,添加第二个text迹后,悬停文本默认会显示迹名称框。希望保留默认悬停模板的样式,但移除迹名称框,但自行尝试的自定义hovertemplate无法还原默认的三角标记,且对非relative类型的柱形显示过多冗余文本。
原瀑布图代码:
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))
尝试的自定义hovertemplate代码(效果不符合预期):
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)
解决方案
plotly瀑布图的默认悬停模板会根据measure的类型动态显示内容,因此需要针对不同measure值定制对应的hovertemplate,再通过ifelse逻辑动态分配,同时添加<extra></extra>来隐藏迹名称框。
核心自定义模板逻辑
根据不同measure类型,匹配默认显示内容:
- relative:显示x轴标签、数值变化(delta)、初始值(initial)以及最终值
- total:仅显示x轴标签和当前总值
- absolute:显示x轴标签和当前数值
对应的hovertemplate设置如下:
hovertemplate = ifelse( data$measure == "relative", "%{x}<br>%{delta}<br>Initial: %{initial}<br>Final: %{y}<extra></extra>", ifelse( data$measure == "total", "%{x}<br>%{y}<extra></extra>", "%{x}<br>%{y}<extra></extra>" ) )
完整修正代码
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)) # 生成动态hovertemplate custom_hover <- ifelse( data$measure == "relative", "%{x}<br>%{delta}<br>Initial: %{initial}<br>Final: %{y}<extra></extra>", ifelse( data$measure == "total", "%{x}<br>%{y}<extra></extra>", "%{x}<br>%{y}<extra></extra>" ) ) (fig <- data %>% plot_ly() %>% add_text(x = factor("Sales", levels = data$x), y = 80, text = "Some Text", # 给text迹也设置空的extra,避免其悬停显示名称 hovertemplate = "%{text}<extra></extra>") %>% add_trace(type = "waterfall", measure = ~measure, x = ~x, textposition = "outside", y= ~y, text =~text, connector = list(line = list(color= "rgb(63, 63, 63)")), hovertemplate = custom_hover) %>% layout(title = "Profit and loss statement 2018", xaxis = list(title = ""), yaxis = list(title = ""), autosize = TRUE, showlegend = FALSE))
说明
- 动态模板完美匹配默认悬停的内容逻辑,不会出现冗余文本
<extra></extra>标签会隐藏迹名称框,同时保留plotly默认的三角标记样式- 额外给
add_text的迹也设置了空的hovertemplate,避免其悬停时显示多余名称
内容的提问来源于stack exchange,提问作者thothal
相关产品推荐
相关产品推荐

