5

I am working on transfering my plots to a presentation using officer package in R.

A ggplot can be transferred using the ph_with_gg() function but what is the function to transfer normal R plots? I am not finding any such function in the package.

These are few of the resouces that I am refering:

officer CRAN package

PowerPoint presentations generation

Your help will be appreciated!

Regards

David Gohel
  • 9,180
  • 2
  • 16
  • 34
Jawairia
  • 295
  • 4
  • 14
  • There must be something that already exists and I just want to know what are those functions simply using officer package. – Jawairia Mar 04 '18 at 06:34
  • I just made a package, export, that allows one to do just that and which is built on top of the officer package, see answer below! – Tom Wenseleers Nov 03 '18 at 23:17

3 Answers3

6

Save the plot you want to output as, say, a png file then use the ph_with_img function to place the image into your presentation.

The on-line help for the Powerpoint component of officer shows how to do this - see https://davidgohel.github.io/officer/articles/powerpoint.html which you already referred to in your post.

If you don't know how to save a plot as an image please see (for example) this thread: how to save a plot as image on the disk

Stewart Ross
  • 1,034
  • 1
  • 8
  • 10
  • 1
    Thanks Stewart for your consideration! Working on code won't be taking place on just one laptop/PC. I am working on a large number of plots, so I don't think saving the plots as an imagine and then calling them to be shown on presentation will help! – Jawairia Mar 04 '18 at 07:57
  • 1
    Glad you found a way forward with rvg. In my own case I use officer and flextable to output to Word, with images stored to temporary locations when needed then incorporated into the word files. All my staff can run this, as the plot images are stored in a common network area accessible to all. The final document includes the images, not links to them, so the document / presentation does not depend on being on any particular PC. Anyway, I'm glad you've found a suitable solution. – Stewart Ross Mar 04 '18 at 10:22
3

I have found a way of doing this using officer and rvg packages together.

rvg also allows us to edit our plots on the slides.

The simple code to save a plot on slides is

ppt <- ph_with_vg(ppt, code = plot(), type = "body")
Jawairia
  • 295
  • 4
  • 14
3

If you would like to export R graphs to Powerpoint you can also use the wrapper package export built on top of officer that just came out on CRAN, see https://cran.r-project.org/web/packages/export/index.html and for demo https://github.com/tomwenseleers/export

Typical syntax is very easy and works for either base R, ggplot2 or lattice graphs, e.g. for a ggplot2 plot (but syntax is the same for base R plot, just call it after you made the plot):

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="ggplot2_plot.pptx", width=6, height=5) 

You can also use it to export to Word, Excel, Latex or HTML and you can also use it to export statistical output of various R stats objects.

Tom Wenseleers
  • 7,535
  • 7
  • 63
  • 103