1

I'm trying to create a ggiraph plot that I would like to include in a rmarkdown document where hovering the legend will also highlight the corresponding points in the plot.

I've tried below code, but while I can hover the points in the plot and the legend they are not linked. Is there a way to link these?

library(ggiraph)
library(ggplot2)

p = ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species, data_id = Species)) +
  geom_point_interactive() +
  scale_color_brewer_interactive(palette = 'Set1',
                                 data_id = function(breaks) as.character(breaks))

p = girafe(ggobj = p)
p = girafe_options(p,
                   opts_hover(girafe_css("stroke:black;fill:black;")),
                   opts_hover_key(girafe_css("stroke:black;fill:black")))
print(p)
Johan
  • 810
  • 6
  • 12

1 Answers1

0

Using the information in @Sidders solution I was able to make this work:

library(ggiraph)
library(ggplot2)

p = ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species, data_id = Species)) +
  geom_point_interactive(aes(`data-id` = Species), extra_interactive_params = "data-id") +
  scale_color_brewer_interactive(extra_interactive_params = "data-id",
                                 `data-id` = unique(iris$Species),
                                 palette = 'Set1',
                                 data_id = function(breaks) as.character(breaks))

p = girafe(ggobj = p)
p = girafe_options(p,
                   opts_hover(girafe_css("stroke:black;fill:black;")),
                   opts_hover_key(girafe_css("stroke:black;fill:black")))
print(p)

Johan
  • 810
  • 6
  • 12