I’m currently trying to export multiple graphs into the same Powerpoint presentation in R. The multiple graphs are created using a function.
However, when I run the code below it produces a separate Powerpoint for each of the variables (I want them in the same one for each of Calc\_Commissiona and CalcCommission\_Perc), Age\_Banded, InstalmentsRequestedInd and NetPrem\_Banded. This is because the ggsave just looks at the last plot I’m assuming.
Any ideas?
Also, the CreateGraph function is just producing the graph for CalcCommission Perc. Both CalcCommission and CalcCommission\_Perc work independently when the other is removed…

```
require(ggplot2)

require(RDCOMClient)

require(R2PPT)

date <- "20160401"

CalcCommission  <- function(Variable,FName,AxisAngle){

    Actual_Commission <- tapply(Converted_A$Commission,Converted_A[Variable],mean)
  Predicted_Commission <- tapply(Final_cut$Commission_Response*Final_cut$Origination.Demand,Final_cut[Variable],sum)/tapply(Final_cut$Origination.Demand,Final_cut[Variable],sum)/100

Data <- data.frame(x=names(Actual_Commission),Actual_Commission,Predicted_Commission)
   Commission_Plot <- ggplot(Data,aes(x=seq(length(unique(x))))) +
    geom_line(aes(y=Actual_Commission, colour = "Actual Commission")) + 
    geom_line(aes(y=Predicted_Commission, colour = "Predicted Commission")) +
    scale_x_continuous(name = FName, 
                       breaks = seq(length(unique(Data$x))), 
                       labels = unique(Data$x))   +
    scale_y_continuous(name = "Commission £") +
    ggtitle("Commission £") +
    theme(legend.title=element_blank(),axis.text.x = element_text(angle = AxisAngle, hjust = 1))

  mypres <- PPT.Init(method="RDCOMClient")
  mypres<-PPT.AddTitleSlide(mypres,title="Commission £",subtitle=date)

  ggsave(my_temp_file<-paste(tempfile(),".wmf",sep=""), plot=Commission_Plot)
  mypres <- PPT.AddBlankSlide(mypres)
  mypres <- PPT.AddGraphicstoSlide(mypres,file=my_temp_file)
  unlink(my_temp_file) 

}

CalcCommission_Perc  <- function(Variable,FName,AxisAngle){
  Actual_Commission_Perc <- tapply((Converted_A$Commission/Converted_A$NetPremium)*100,Converted_A[Variable],mean)

Predicted_Commission_Perc <- (((tapply(Final_cut$Commission_Response*Final_cut$Origination.Demand,Final_cut[Variable],sum)/tapply(Final_cut$Origination.Demand,Final_cut[Variable],sum))/100)/
                                  (tapply(Final_cut$Prem_Net*Final_cut$Origination.Demand,Final_cut[Variable],sum)/tapply(Final_cut$Origination.Demand,Final_cut[Variable],sum)))*100

Data <- data.frame(x=names(Actual_Commission_Perc),Actual_Commission_Perc,Predicted_Commission_Perc)

  Commission_Perc_Plot <- ggplot(Data,aes(x=seq(length(unique(x))))) +
    geom_line(aes(y=Actual_Commission_Perc, colour = "Actual Commission %")) + 
    geom_line(aes(y=Predicted_Commission_Perc, colour = "Predicted Commission %")) +
    scale_x_continuous(name = FName, 
                       breaks = seq(length(unique(Data$x))), 
                       labels = unique(Data$x))   +
    scale_y_continuous(name = "Commission £") +
    ggtitle("Commission %") +
    theme(legend.title=element_blank(),axis.text.x = element_text(angle = AxisAngle, hjust = 1))

  mypres <- PPT.Init(method="RDCOMClient")
  mypres<-PPT.AddTitleSlide(mypres,title="Commission %",subtitle=date)

  ggsave(my_temp_file<-paste(tempfile(),".wmf",sep=""), plot=Commission_Perc_Plot)
  mypres <- PPT.AddBlankSlide(mypres)
  mypres <- PPT.AddGraphicstoSlide(mypres,file=my_temp_file)
  unlink(my_temp_file) 
  }

CreateGraph  <- function(Variable,FName,AxisAngle){

  CalcCommission(Variable,FName,AxisAngle)
  CalcCommission_Perc(Variable,FName,AxisAngle)
  }
CreateGraph("Age_Banded","Age",0)
CreateGraph("InstalmentsRequestedInd","DD Payment",0)
CreateGraph("NetPrem_Banded","Net Premium",45)

```