library(tidyverse)

Experiment

library(ggiraph)
plot <- ggplot(cars) + 
  aes(speed, dist) + 
  geom_point_interactive() + 
  aes(tooltip = speed)

ggiraph::girafe(plot)
make_interactive <- function(data = NULL) {

  structure(
    list(), 
    class = "interactive"
    )

}

ggplot_add.interactive <- function(object, plot, object_name) {
  
  ggiraph::girafe(plot)

}

ggplot(cars) + 
  aes(speed, dist) + 
  geom_point_interactive() + 
  aes(tooltip = speed) +
  make_interactive()
ggplot(cars) + 
  aes(speed, dist) + 
  geom_point_interactive()

p <- last_plot()


ggplot(cars) + 
  aes(speed, dist) + 
  geom_point()

p$layers
## $geom_interactive_point
## geom_interactive_point: na.rm = FALSE, .ipar = c("data_id", "tooltip", "onclick", "hover_css", "selected_css", "tooltip_fill", "hover_nearest")
## stat_identity: na.rm = FALSE
## position_identity
last_plot()$layers
## $geom_point
## geom_point: na.rm = FALSE
## stat_identity: na.rm = FALSE
## position_identity
tooltip <- function(tooltip) {

  structure(
    list(tooltip_specs = rlang::enquo(tooltip)), 
    class = "tt"
    )

}

ggplot_add.tt <- function(object, plot, object_name) {
  
  plot <- plot + aes(tooltip = !!object$tooltip_specs)
  
  ggiraph::girafe(plot)

}

mtcars |>
  rownames_to_column("car") |>
  ggplot() + 
  aes(mpg, disp, color = cyl) + 
  geom_point_interactive() + 
  aes(size = cyl) +
  tooltip(car)

ToDo S7

S7::method(update_ggplot, list(element_text, class_ggplot)) <-
  function(object, plot, ...) {
    plot + theme(text = object)
  }

# we can now use `+` to add our object to a plot
ggplot(mpg, aes(displ, cty)) +
  geom_point() +
  element_text(colour = "red")

Closing remarks, Other Relevant Work, Caveats