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

ggiraph折线图交互时多条线条意外高亮问题求助

问题:ggiraph交互式折线图悬停时多线条意外高亮

使用ggiraph制作Shiny交互式折线图时,设置data_id=Regionname后,悬停某一地区的线条时,其他包含相同子串的地区线条也会被高亮(比如悬停"Southern Metropolitan"时,"Western Metropolitan"和"Western Victoria"也被高亮)。

原因

ggiraph默认采用子字符串匹配判断交互关联,只要data_id包含相同片段就会被归为一组;另外你的geom_point_interactive未绑定对应data_id,会进一步导致交互识别混乱。

修复方案

1. 给点元素绑定data_id

确保geom_point_interactive和线条使用同一个data_id,让点和对应线条属于同一交互组:

geom_point_interactive(aes(data_id=Regionname))

2. 启用精确匹配模式

在girafe()中设置交互选项,强制只匹配完全一致的data_id:

girafe(
  ggobj=obj,
  options = list(
    opts_hover(css = "stroke-width:2;"),
    opts_hover_inv(css = "opacity:0.3;"),  # 非高亮元素半透明
    opts_selection(type = "single"),
    opts_hover_mode(mode = "exact")  # 关键:精确匹配data_id
  )
)

完整修复代码

library(shiny)
library(ggiraph)

tab <- tabPanel(
  title='Analysis',
  mainPanel(girafeOutput("plotLineChart"))
)

ui <- navbarPage(
  title='title',
  tab
)

server <- function(input, output, session) {

  output$plotLineChart <- renderGirafe({
    
    avg_price_by_region_year <- data.frame(
        Regionname = c("Eastern Metropolitan", "Eastern Metropolitan", "Eastern Metropolitan",
                        "Eastern Victoria", "Eastern Victoria",
                        "Northern Metropolitan", "Northern Metropolitan", "Northern Metropolitan",
                        "Northern Victoria", "Northern Victoria",
                        "South-Eastern Metropolitan", "South-Eastern Metropolitan", "South-Eastern Metropolitan",
                        "Southern Metropolitan", "Southern Metropolitan", "Southern Metropolitan",
                        "Western Metropolitan", "Western Metropolitan", "Western Metropolitan",
                        "Western Victoria", "Western Victoria"),
        Year = as.factor(c(2016, 2017, 2018,
                            2017, 2018,
                            2016, 2017, 2018,
                            2017, 2018,
                            2016, 2017, 2018,
                            2016, 2017, 2018,
                            2016, 2017, 2018,
                            2017, 2018)),
        AvgPrice = c(1109674.0, 1158106.8, 1240989.0,
                    655853.2, 686400.0,
                    887806.8, 868906.8, 879591.4,
                    577051.3, 636326.1,
                    911577.0, 931217.6, 891513.5,
                    1400595.8, 1522924.0, 1953786.1,
                    877687.7, 876664.7, 886901.0,
                    437528.6, 482000.0)
        )

    obj <- ggplot(avg_price_by_region_year, aes(x=Year, y=AvgPrice, group=Regionname, color=Regionname)) +
      geom_line_interactive(aes(data_id=Regionname)) + 
      geom_point_interactive(aes(data_id=Regionname)) +  # 给点添加data_id
      labs(x="Year", y="Average Price") +
      scale_color_viridis_d()
    
    girafe(
      ggobj=obj,
      options = list(
        opts_hover(css = "stroke:black; stroke-width:2;"),  # 高亮样式
        opts_hover_inv(css = "opacity:0.3;"),  # 非高亮元素变暗
        opts_selection(type = "single", css = "stroke:black; stroke-width:2;"),
        opts_hover_mode(mode = "exact")  # 开启精确匹配
      )
    )
  })
}

shinyApp(ui, server)

效果说明

  • 悬停任意地区的线条或点时,只有对应地区的线条和点会高亮,其他元素会半透明;
  • 精确匹配模式彻底避免了子串导致的误高亮问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 10:05:54