Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

creating multiple editable ggplots in powerpoint

I am trying to create a way to take a list of ggplots and turn them into a powerpoint with each slide being an editable ggplot from the list. Below I provided the code I have and explained in the notes where the issue occurs aka during the loop. It seems like it is overriding itself when looping.

library(dplyr)
library(tidyverse)
library(officer)
library(rvg)

#create example data

df <- data.frame(country = c(rep('USA',20), rep('Canada',20), rep('Mexico',20)),

                 wave = c(1:20, 1:20, 1:20),

                 par = c(1:20 + 5*runif(20), 21:40 + 10*runif(20), 1:20 + 15*runif(20)))

 

countries <- unique(df$country)

#make list of plots

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

plot_list <- list()

i <- 1

 

for (c in countries){

  pl <- ggplot(data = df %>% filter(country == c)) +

    geom_point(aes(wave, par), size = 3, color = 'red') +

    labs(title = as.character(c), x = 'wave', y = 'value') +

    theme_bw(base_size = 16)

 

  plot_list[[i]] <- pl

  i <- i + 1

}

#plot_list is the list of plots 
plot_list

 

#convert list of plots into editable vector graphic objects

myplots <-  dml(ggobj = plot_list

                ,bg = "white"

                ,pointsize = 12

                ,editable = TRUE)
 

### now create loop ---- this is were I get issues. it seems like the plots are just overriding one another during the loop and not creating a new slide

{

  for(plot in 1:length(plot_list)) {

    doc <- read_pptx()

    doc <- add_slide(doc, "Title and Content", "Office Theme")

    doc <- ph_with(doc, plot_list[[plot]], location = ph_location_fullsize())

    fileout <- "vectorGraphics.pptx"

    print(doc, target = fileout)

  }

}

>Solution :

First, I would suggest to use lapply to create your list of plots. Second, to convert your plots to dml objects you have to loop over the list of plots. Finally, if you want one pptx with each plot on a separate slide, create and export the pptx outside of the for loop:

library(ggplot2)
library(officer)
library(rvg)

plot_list <- df |>
  split(~country) |>
  lapply(\(x) {
    ggplot(x) +
      geom_point(aes(wave, par), size = 3, color = "red") +
      labs(title = unique(x$country), x = "wave", y = "value") +
      theme_bw(base_size = 16)
  })

plot_list_dml <- lapply(
  plot_list, \(x)
  dml(
    ggobj = x,
    bg = "white",
    pointsize = 12,
    editable = TRUE
  )
)

doc <- read_pptx()
for (plot in plot_list_dml) {
  doc <- add_slide(doc, "Title and Content", "Office Theme")

  doc <- ph_with(doc, plot, location = ph_location_fullsize())
}

fileout <- "vectorGraphics.pptx"

print(doc, target = fileout)

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading