rggplot2officerr-flextable

grouping a table and a ggplot object and export to pptx as a editable graphic (svg converted to a forms)


I want to produce a pptx slide like this:

library(ggplot2)
library(grid)
library(cowplot)
library(dplyr)
# remotes::install_github("davidgohel/flextable")
library(flextable)
    
   gg1 <- ggplot(iris, aes(Sepal.Length, Petal.Length, color = Species) ) + geom_point()

  ft_raster <- iris %>% group_by(Species) %>% 
  summarise_all(median) %>% 
  flextable() %>% autofit() %>% 
  as_raster()

gg2 <- ggplot() + 
  theme_void() + 
  annotation_custom(rasterGrob(ft_raster), xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf)

complete_plot <- cowplot::plot_grid(gg1, gg2, nrow = 2, ncol = 1, rel_heights = c(3, 1) )

doc <- read_pptx()
doc <- add_slide(doc, layout = "Title and Content", master = "Office Theme")
doc <- ph_with(doc, complete_plot, location = ph_location_fullsize() )
print(doc, target = "my_plot.pptx")

but being able to edit some values and colors after in the power point presentation. there are a way to export an editable version of this plots?


Solution

  • Have a look at the rvg package. It is meant for this use case and you can edit the figure at least to some degree.

    library(rvg)
    complete_plot_dml <- rvg::dml(ggobj = complete_plot)
    
    doc <- read_pptx()
    doc <- add_slide(doc, layout = "Title and Content")
    doc <- ph_with(doc, complete_plot_dml, "fullsize")
    
    f <- print(doc, target = tempfile(fileext = ".pptx"))
    browseURL(f)
    

    Also, you may want to consider adding the flextable below the figure (but not as a raster) so it can be edited.