Plots can be saved in a variety of formats as described and subsequently imported into Microsoft PowerPoint of Word. A vector format would be best for this purpose. However, if there are many plots, this becomes a tedious task. It would be preferable to save plots directly to a Microsoft PowerPoint presentation in vector format.
Tom Wenseleers 1, has created a function for this (Export2Office: export2ppt and export2doc). This function can be copied and pasted into the console window and subsequently executed. The library ReporteRs 2 and its dependencies should be installed. A Microsoft PowerPoint or Word document is created in the default directory as selected / defined in the setup.
Example 1: Create a PowerPoint slide using Tom Weseleers 1 function
A: First create a simple plot with 100 random numbers of a Normal distribution with a mean of 2 and a standard deviation of 1:
plot(rnorm(100,mean=2,sd=1))
B: Next, select all (CTRL-A) Tom Wenseleers Export2Office function, copy (CTRL-C) and paste (CTRL-V) it in the console and hit return. To create a powerpoint presentation called ‘oneslide.pptx’ in the default directory:
export2ppt(file=’oneplot.pptx’)
Loading required package: ReporteRs
Loading required package: ReporteRsjars
Warning message:
argument fontname is deprecated; please usefontname_serif, fontname_sans ,fontname_mono,fontname_symbol instead.
Similarly, to create a word document with the plot as a vector file:
export2doc(file=’oneplot.docx’)
Warning message:
argument fontname is deprecated; please usefontname_serif, fontname_sans ,fontname_mono,fontname_symbol instead.
As described in here, the aspect ratio etc can also be specified.
It is also possible to import Tom Wenseleers library into R 1, but this is a little different than described in the install packages page. This library is particularly useful if several plots have been created in different plot windows and all plots are to be included into a single PowerPoint presentation.
To install Tom Wenseleers export package 1:
This is described on https://github.com/tomwenseleers/export. The following libraries should be installed (dependencies):
install.packages("rJava") install.packages("ReporteRs") install.packages("ReporteRsjars") install.packages("ggplot2") install.packages("rtable") install.packages("xtable") install.packages("taRifx") install.packages("stargazer") install.packages("tikzDevice")
install.packages("devtools") library(devtools) devtools::install_github('tomwenseleers/export',local=F)
To import the library:
library(export)
Help can be found by entering:
?export2ppt
or
?export2doc
as shown on the website.
Example 2: Insert several different plots into one PowerPoint file
A: Give each plot window a ‘handle’, so that you can refer to the plot window by the handle. A new plot window is called by the dev.new() function. The plot name (handle) of the current plot window can be set with the dev.cur() function. Switching between active plots is done by dev.set(plotname). This example uses three simple plots, but the same methods can be used with more complicated plots created with ggplot2 3.
dev.new()
plot1<-dev.cur()
dev.new()
plot2<-dev.cur()
dev.new()
plot3<-dev.cur()
dev.set(plot1)
quartz
2
Create a plot with 100 random elements from a normal distribution with a mean of two and a standard deviation of 1:
plot(rnorm(100,mean=2,sd=1))
dev.set(plot2)
quartz
3
Create a plot with 100 random elements of a chi square distribution with two degrees of freedom:
plot(rchisq(100,df=2))
dev.set(plot3)
quartz
4
Create a plot with 100 sequential numbers:
plot(1:100)
B: Use the export function to create a Microsoft PowerPoint presentation in the default folder / directory that contains all the three plots. Each plot will be added as a new slide to the presentation:
library(export)
Loading required package: ReporteRs
Loading required package: ReporteRsjars
Warning message:
replacing previous import ‘ReporteRs::renderFlexTable’ by ‘rtable::renderFlexTable’ when loading ‘export’
dev.set(plot1)
quartz
2
graph2ppt(file=’threeplots.pptx’)
Exported graph as threeplots.pptx
Warning message:
argument fontname is deprecated; please usefontname_serif, fontname_sans ,fontname_mono,fontname_symbol instead.
dev.set(plot2)
quartz
3
graph2ppt(file=’threeplots.pptx’, append = TRUE)
Exported graph as threeplots.pptx
Warning message:
argument fontname is deprecated; please usefontname_serif, fontname_sans ,fontname_mono,fontname_symbol instead.
dev.set(plot3)
quartz
4
graph2ppt(file=’threeplots.pptx’, append = TRUE)
Exported graph as threeplots.pptx
Warning message:
argument fontname is deprecated; please usefontname_serif, fontname_sans ,fontname_mono,fontname_symbol instead.
The file ‘threeplots.pptx’ should be created in the default directory (where R saves its files by default). If you are not sure where that is, just search your computer’s hard drive for the file ‘threeplots.pptx’.