The ggplot_image() function

Create a ggplot plot.

library(ggplot2)

plot_object <-
  ggplot(
    data = gtcars,
    aes(x = hp, y = trq, size = msrp)
  ) +
  geom_point(color = "blue") +
  theme(legend.position = "none")

Create a tibble that contains two cells (where one is a placeholder for an image), then, create a gt table. Use the text_transform() function to insert the plot using by calling ggplot_object() within the user- defined function.

dplyr::tibble(
  text = "Here is a ggplot:",
  ggplot = NA
) |>
  gt() |>
  text_transform(
    locations = cells_body(columns = ggplot),
    fn = function(x) {
      plot_object |>
        ggplot_image(height = px(200))
    }
  )
text ggplot
Here is a ggplot: