You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R语言Plotly中关闭add_trace的悬停提示保留主折线悬停?

解决Plotly中关闭指定Trace的悬停提示问题

我想了解是否可以在Plotly图表输出中不显示add_trace添加元素的悬停值?以下代码生成了一系列年度累积折线图,通过add_trace添加了一条红色虚线竖线,但悬停时会显示该竖线的数值。能否关闭该竖线的悬停提示,同时保留年度累积折线的悬停提示?

只需要在添加红色竖线的add_trace()函数中加入hoverinfo = 'none'参数,就能单独禁用这条竖线的悬停提示,不会影响原有年度累积折线的悬停效果。

修改后的完整代码如下:

df %>% 
  filter(op_area_ocode == 'ALL') %>% mutate(Year = as.character(R.Year), DoY = as.Date(DoY.N, origin = "1972-01-01") - 1) %>% 
  plot_ly(., x = ~ DoY, y = ~cni, color = ~Year, colors = c("#D3D3D3","#D3D3D3","#D3D3D3","#D3D3D3","black","#E69F00","#56B4E9", "#009E73", "#AA4499"), type = 'scatter', mode = 'lines') %>%
  layout(title = paste("Reports"), xaxis = list(title = "", type = "date", tickformat = "%d %b"), yaxis = list (title = "Number of reports")) %>% 
  add_trace(x = as.Date("1972-08-15"), type = 'scatter', mode = 'lines', line = list(color = 'red', width=0.5, dash ="dot"), name = '', showlegend = FALSE, hoverinfo = 'none') 

说明

  • hoverinfo = 'none'是Plotly的内置参数,用于控制单个trace的悬停信息显示状态,设置为'none'后,该trace将不会触发任何悬停提示,完全符合你的需求。

内容的提问来源于stack exchange,提问作者Drew

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 17:12:22