Search code examples
rpowerpointgrid-layoutflextableofficer

group a flextable, a ggplot, a text and an image together into a grid object, then add it to a powerpoint slide


I want to know if it's possible to group a flextable/regulartable, a ggplot, a text and an image together into a grid object, and then put that grid in a PowerPoint slide?

This could be very useful for arranging the objects inside a power point slide without being forced to calculate always the coordinates of every object.

I saw an example with combining and arranging ggplot objects through grid.arrange (r - Why can't I send a group of plots in a grid to PowerPoint with OfficeR?) But if I want to add also a flextable or maybe a new text paragraph, it doesn't work anymore.

Is there a way to improve the presentation layer whit a grid layout?

Thank you!


Solution

  • It is possible since version 0.5.3 of flextable. There are many ways to achieve that, here is one example:

    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)
    
    cowplot::plot_grid(gg1, gg2, nrow = 2, ncol = 1, rel_heights = c(3, 1) )
    

    And the result below

    enter image description here