Search code examples
rggiraph

Hover aesthetic doesn't work in ggiraph::girafe()


The static image below can't show it, but when I hover over the interactive plot produced by girafe(ggobj = plt), all of the tooltips say 0. How can I make them show the same value as x?

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)
library(ggiraph)

plt <-
  ChickWeight %>% 
    as_tibble() %>% 
    filter(Chick == 1) %>% 
    ggplot(
      aes(
        x = Time,
        y = weight
      )
    ) +
    geom_line_interactive(
      aes(
        tooltip = Time
      )
    )

girafe(ggobj = plt)

Created on 2024-07-28 with reprex v2.0.2


Solution

  • From my understanding and after some experimenting you will get only one tooltip for a line. Only when mapping on another aesthetic, e.g. when you add color = Time (with group = 1) will the single segments of the line treated separately and can you have a separate tooltip.

    Perhaps there is a straight-forward approach to achieve your desired result, but a workaround would be to use e.g. an invisible points layer:

    library(dplyr)
    library(ggplot2)
    library(ggiraph)
    
    plt <-
      ChickWeight %>%
      as_tibble() %>%
      filter(Chick == 1) %>%
      ggplot(
        aes(
          x = Time,
          y = weight
        )
      ) +
      geom_point_interactive(
        aes(tooltip = Time),
        color = "transparent"
      ) +
      geom_line_interactive()
    
    girafe(ggobj = plt)
    

    enter image description here