rggplot2ggiraph

Exporting a graph in svg format, which is a `girafe` object (`ggiraph` package)


ggiraph package is suitable for generating interactive graphics.

I was trying to execute some examples found online concerning package ggiraph and I was not able to find anywhere how to export the interactive graph generated, maybe in format svg (or other).

Below the code:

library(ggplot2)
library(ggiraph)
library(patchwork)
library(cowplot)

mtcars <- mtcars
mtcars$tooltip <- rownames(mtcars)

gg1 <- ggplot(mtcars) +
  geom_point_interactive(aes(x = drat, y = wt, color = qsec, 
    tooltip = tooltip, data_id = tooltip ), size = 4) 

gg2 <- ggplot(mtcars) +
  geom_point_interactive(aes(x = qsec, y = disp, color = mpg, 
    tooltip = tooltip, data_id = tooltip ), size = 4)

Then I tried to export it:

oggetto <- girafe( ggobj = plot_grid(gg1, gg2), width_svg = 8, height_svg = 4)
library(svglite)
ggsave(file="test.svg", plot=oggetto, width=10, height=8)

getting the following error:

Error in UseMethod("grid.draw") : 
  no applicable method for 'grid.draw' applied to an object of class "c('girafe', 'htmlwidget')"

Does somebody knows how to export the images?

Thank you in advance for every eventual reply! Giacomo


Solution

  • You can save the interactive plot containing the svg in a HTML file using htmltools::save_html(oggetto, "out.html")

    out.html contains JavaScript e.g. ggiraphjs.min.js required to run the interactivity. Plain SVG files can display tooltips using the title attribute, but the orange point color on hover on both plots simultaneously seems to require JavaScript.