ggiraph font size HTML

51 Views Asked by At

I am using ggiraph with geom_tile_interactive in RStudio. The graph renders beautifully in the viewer window but after converting to HTML & hovering over the graph online, the font size is too tiny to read. I am using ggiraph 0.8.7 and the latest CRAN version of htmlwidgets. This is my code

g <- ggplot(gg.dat, aes(x = class, y = var.lab, fill = probability,
                        tooltip = paste0(class, "\n", var.lab, "\n", probability),
                                       data_id = var.lab)) +
         geom_tile_interactive(width = .75)

    g.int <- girafe(ggobj = g)

    saveWidget(g.int, file = paste0(my.dir, "/", my.graph.name, ".html), 
               selfcontained = TRUE)

Any assistance would be greatly appreciated.

Don

1

There are 1 best solutions below

1
stefan On

Whereas you can set the font size for the chart itself using theme() or size= as in ggplot2 for the tooltip you have to set the font size using the options= argument of girafe(). To this end ggiraph provides a helper function opts_tooltip() which allows to style the tooltip via the css= argument, e.g. in the code below I have set the font-size to 4rem.

Using a minimal reproducible example based on mtcars:

library(ggiraph)
library(ggplot2)
library(dplyr, warn = FALSE)

gg.dat <- mtcars |>
  group_by(class = factor(cyl), var.lab = factor(am)) |>
  summarise(
    probability = mean(mpg),
    .groups = "drop"
  )

g <- ggplot(gg.dat, aes(
  x = class, y = var.lab, fill = probability,
  tooltip = paste0(class, "\n", var.lab, "\n", probability),
  data_id = var.lab
)) +
  geom_tile_interactive(width = .75) +
  theme(text = element_text(size = 20))

g.int <- girafe(
  ggobj = g,
  options = list(
    opts_tooltip(
      use_fill = TRUE,
      css = "padding:5pt;font-size:4rem;color:white"
    )
  )
)

htmlwidgets::saveWidget(g.int,
  file = "foo.html",
  selfcontained = TRUE
)

enter image description here