HeatherS
HeatherS

Reputation: 29

exporting to powerpoint using a function to create graphs

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)

Upvotes: 2

Views: 1084

Answers (2)

Tom Wenseleers
Tom Wenseleers

Reputation: 7989

Answer above is outdated, as ReporteRs has been removed from CRAN and is superseded by officer. I just made a new package export built on top of officer that easily allows one to export several graphs to a single Powerpoint presentation using the graph2ppt() command and the append=TRUE option, e.g. to produce a presentation with 2 slides :

install.packages("export")
library(export)
library(ggplot2)
qplot(Sepal.Length, Petal.Length, data = iris, color = Species, 
      size = Petal.Width, alpha = I(0.7))     
graph2ppt(file="plots.pptx", width=6, height=5) 
qplot(Sepal.Width, Petal.Length, data = iris, color = Species, 
      size = Petal.Width, alpha = I(0.7))     
graph2ppt(file="plots.pptx", width=6, height=5, append=TRUE) 

Upvotes: 0

lukeA
lukeA

Reputation: 54237

Here's one way to save two plots in one pptx file:

library(ReporteRs)
library(ggplot2)
library(magrittr)
pptx() %>%
  addSlide("Title and Content") %>% 
  addTitle("plot #1") %>% 
  addPlot(function() barplot( 1:8, col = 1:8 )) %>% 
  addSlide("Title and Content") %>% 
  addTitle("plot #2") %>% 
  addPlot(fun = print, x = qplot(Sepal.Length, Petal.Length, data = iris, color = Species, size = Petal.Width, alpha = I(0.7) )) %>%
  writeDoc(ppfn <<- tempfile(fileext = ".pptx"))

ppfn contains the PowerPoint file name including its path. Check out the package documentation here.

Upvotes: 2

Related Questions