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

ggiraph::girafe()交互图表hover tooltip显示0异常,如何解决?

解决ggiraph交互式图表tooltip显示0的问题

问题出在geom_line_interactive默认将整个线条作为单个交互式元素,只会读取数据中第一个tooltip值(也就是Time的初始值0)。要让tooltip显示对应位置的Time值,有两种常用解决方法:

方法1:添加交互式点元素

为每个数据点添加geom_point_interactive,将tooltip绑定到点上,悬停时点会显示对应的Time值:

library(dplyr)
library(ggplot2)
library(ggiraph)

plt <-
  ChickWeight %>% 
    as_tibble() %>% 
    filter(Chick == 1) %>% 
    ggplot(
      aes(
        x = Time,
        y = weight
      )
    ) +
    geom_line_interactive() +  # 保留原线条
    geom_point_interactive(
      aes(tooltip = Time)  # 为每个点绑定Time作为tooltip
    )

girafe(ggobj = plt)

方法2:让线条的每个分段对应独立tooltip

如果不想显示点,可以将数据转换为线段格式,用geom_segment_interactive为每个线段绑定对应Time值:

library(dplyr)
library(ggplot2)
library(ggiraph)

plt_data <- ChickWeight %>% 
  as_tibble() %>% 
  filter(Chick == 1) %>% 
  mutate(
    next_time = lead(Time),
    next_weight = lead(weight)
  ) %>% 
  filter(!is.na(next_time))

plt <- ggplot(plt_data) +
  geom_segment_interactive(
    aes(
      x = Time, xend = next_time,
      y = weight, yend = next_weight,
      tooltip = Time
    )
  )

girafe(ggobj = plt)

两种方法中,方法1更直观,用户悬停在数据点上就能看到对应Time值,也是更常用的解决方案。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 06:00:08