How do I make the `ggiraph` hover functionality stick on click?

466 Views Asked by At

I have a visualization (see below) that has nice hover functionality that makes readability much easier. I would like the user to be able to make the hover functionality persist by clicking the thing they are hovering over. Is this possible using ggiraph?

Minimal reproducible example code:

library(tidyverse)
library(ggiraph)

nodes = tibble(value=c(1,2,1,2), week=c(1,1,2,2))     
vertices = tibble(From=c(1,1,2,2), To=c(1,2,1,2), weight=c(.5,.5,.5,.5), start_week=1, rand=c(.48, .33, .34,.40))
p=ggplot(nodes) +
  geom_point_interactive(size = 12, aes(x=week, y=value, color =paste(week, value), data_id=paste(week, value)))+
  geom_text_interactive(color="white",aes(x=week, y=value, label=value, data_id=paste(week, value)))+
  geom_segment_interactive(data=vertices, aes(x=start_week+.1, xend=start_week+1-.1, y=From, yend=To,
                                                                     data_id=paste(start_week, From)))+
  geom_text_interactive(data=vertices, aes(x=start_week+rand, y=From-(From-To)*rand, label=paste0(round(weight*100), "%"),
                                                                  data_id=paste(start_week, From)), vjust=-.1, hjust=-.1) +
  guides(color=F, size=F)+
  scale_y_continuous(name="Cluster", breaks=NULL, labels=NULL)+
  scale_x_continuous(name="Stage", breaks=1:6)+
  theme_minimal()

girafe(ggobj=p,
       options = list(
         opts_hover_inv(css = "opacity:0.1;"),
         opts_hover(css = "stroke-width:2;")
       ))

enter image description here

1

There are 1 best solutions below

2
On

You will probably have to tune some options to make it prettier. Here the important point is to define opts_selection(only_shiny = FALSE) so that selection is also possible without shiny:

library(tidyverse)
library(ggiraph)

nodes = tibble(value=c(1,2,1,2), week=c(1,1,2,2))     
vertices = tibble(From=c(1,1,2,2), To=c(1,2,1,2), weight=c(.5,.5,.5,.5), start_week=1, rand=c(.48, .33, .34,.40))
p=ggplot(nodes) +
  geom_point_interactive(size = 12, aes(x=week, y=value, color =paste(week, value), data_id=paste(week, value)))+
  geom_text_interactive(color="white",aes(x=week, y=value, label=value, data_id=paste(week, value)))+
  geom_segment_interactive(data=vertices, aes(x=start_week+.1, xend=start_week+1-.1, y=From, yend=To,
                                              data_id=paste(start_week, From)))+
  geom_text_interactive(data=vertices, aes(x=start_week+rand, y=From-(From-To)*rand, label=paste0(round(weight*100), "%"),
                                           data_id=paste(start_week, From)), vjust=-.1, hjust=-.1) +
  guides(color=F, size=F)+
  scale_y_continuous(name="Cluster", breaks=NULL, labels=NULL)+
  scale_x_continuous(name="Stage", breaks=1:6)+
  theme_minimal()

girafe(ggobj=p,
       options = list(
         opts_hover_inv(css = "opacity:0.1;"),
         opts_hover(css = "stroke-width:2;"),
         opts_selection(only_shiny = FALSE, type = "single", css = "stroke:yellow;")
       ))