1

ggiraph creates much less issues converting a ggplot2 graphic to an interactive htmlwidget compared to plotly::ggplotly(). However, there is one key feature of plotly which I am missing in ggiraph: Selecting points by name in a selection box (via argument selectize in plotly::highlight())

library(plotly)
data("ChickWeight")
cw <- highlight_key(ChickWeight, ~Chick, group = "Chicken")
p <-  ggplot(cw, aes(x = Time, y = weight, col = Diet, group = Chick)) +
  geom_line() + geom_point() 
pp <- ggplotly(p, tooltip = c("Chick"))
highlight(pp, on = "plotly_hover", selectize = TRUE)

enter image description here

Is there a way I can achieve this with ggiraph (without creating a shiny app and running a live R session)?

Code I use so far:

library(ggiraph)
set_girafe_defaults(opts_hover_inv = opts_hover_inv(css = "opacity:0.1;"))
p <-  ggplot(ChickWeight, aes(x = Time, y = weight,
                              colour = Diet, group = Chick,
                              tooltip = Chick, data_id = Chick)) +
  geom_line_interactive() + geom_point_interactive()
girafe(ggobj = p)

Thanks for your help!

retodomax
  • 434
  • 3
  • 14

1 Answers1

1

There is no interactive box available without shiny but you can init a girafe output with a selection:

library(ggplot2)
library(ggiraph)
set_girafe_defaults(opts_hover_inv = opts_hover_inv(css = "opacity:0.1;"))
p <-  ggplot(ChickWeight, aes(x = Time, y = weight,
                              colour = Diet, group = Chick,
                              tooltip = Chick, data_id = Chick)) +
  geom_line_interactive() + geom_point_interactive()
girafe(ggobj = p, options = list(
  opts_selection(
    selected = c("47", "48"), 
    css = girafe_css(css = "", point = "fill:red;r:5px;", 
                     area = "", line = "stroke:black;stroke-width:3px;"),
    type = "multiple", 
    only_shiny = FALSE)
))

enter image description here

David Gohel
  • 9,180
  • 2
  • 16
  • 34
  • Thanks David! Good to know that specific lines can be pre-selected. Still, if it is ever planned to add additional features to ggiraph, a selection box would be highly appreciated. – retodomax Jul 05 '23 at 08:23