ggiraph: fixed tooltip in geom_line_interactive

568 Views Asked by At

I'm probably missing something really, really obvious... In Example 2 the hovertext show '1', the first value of the assigned tooltip column and does not change

I want to have a changing hover text like in example 1 without assinging f.e. color to y (like in example 2)

  1. with color = y
testdf <- data.frame(
  "x" = c(1:10),
  "y" = c(1:10))

test <- testdf %>% 
  ggplot2::ggplot() + 
  ggiraph::geom_line_interactive(
    ggplot2::aes(x = x,
                 y = y,
                 color = y,
                 tooltip = y)
  )

ggiraph::girafe(ggobj = test,
                height_svg = 3.5,
                options = list(
                  opts_sizing(rescale = T),
                  # Gestaltung des tooltips
                  opts_tooltip(css= "font-family: Helvetica; padding:3pt; color:white; background-color:#15253F; border-radius:5px"),
                  # Zoom
                  opts_zoom(max = 5),
                  # Hover Optionen 
                  opts_hover(css = "stroke-width:2;"),
                  opts_hover_inv(css = "opacity:0.8"),
                  opts_toolbar(position = "topright", saveaspng = F) 
                  
                ))
  1. Without color = y
testdf <- data.frame(
  "x" = c(1:10),
  "y" = c(1:10))

test <- testdf %>% 
  ggplot2::ggplot() + 
  ggiraph::geom_line_interactive(
    ggplot2::aes(x = x,
                 y = y,
                 tooltip = x)
  )

ggiraph::girafe(ggobj = test,
                height_svg = 3.5,
                options = list(
                  opts_sizing(rescale = T),
                  # Gestaltung des tooltips
                  opts_tooltip(css= "font-family: Helvetica; padding:3pt; color:white; background-color:#15253F; border-radius:5px"),
                  # Zoom
                  opts_zoom(max = 5),
                  # Hover Optionen 
                  opts_hover(css = "stroke-width:2;"),
                  opts_hover_inv(css = "opacity:0.8"),
                  opts_toolbar(position = "topright", saveaspng = F) 
                  
                ))
1

There are 1 best solutions below

0
On

One solution would be to add a geom_point_interactive to represent the values. You can hide them by making the point very small (size = 0.05).

geom_point_interactive(aes(x = x,
             y = y,
             tooltip = x,
             data_id = x), size = 0.05)

Presents value of x

If you also want to have some information on the overall line, you can add this as a different variable in the geom_line_interactive aes() i.e. tooltip = "My line":

geom_line_interactive(aes(x = x,
             y = y,
             tooltip = "My line",
             data_id = x)) +
geom_point_interactive(aes(x = x,
             y = y,
             tooltip = x,
             data_id = x), size = 0.05)

enter image description here