简体   繁体   中英

Embed a graph into an existing pptx slide using the "ph_with_vg_at" R function

I have an R applicaion that builds a large number of client editable charts by Office tool (pptx). I try to embed a ggplot graph into an existing slied in vector format. I use the ph_with_vg_at function to export the graph to pptx. The point is that exporting the graph to pptx deletes all that elements that already exist on the slide. How then can I export the graph to a slide without deleting the information on the slide?

Thank's Joni

OK

It turns out to be simple if you understand the syntax... Let's take a simple ggplot for example:

require("officer")
require("rvg")
require("ggplot2")

gg <- ggplot(mtcars, aes(x = mpg , y = wt, colour = qsec)) + geom_point() + theme_minimal()

option 1: Opening an existing presentation and insert a new object (while deleting the objects in the presentation):

doc <- read_pptx() # Creat a virtual new pptx and set the ggplot as a new slide
doc <- add_slide(doc, layout = "Title and Content", master = "Office Theme")
doc <- ph_with_vg(doc, code =print(gg),type = "body")


print(doc, target = "G:/Layers/gg.pptx") # Exporting to Exist pptx file

option 2: Opening an existing presentation and embed an object in existing slide without deleting the existing objects on the slide:

doc <- read_pptx(path = "G:/Layers/template.pptx")# Opening an Exist pptx file


doc <- on_slide( doc, index = 1)# Embedding of the ggplot into the slide
doc <- ph_with_vg_at(doc, ggobj = gg, left=0.1, top=0.1, width=7.5, height=6)

   print(doc, target = "G:/Layers/template.pptx")# Exporting to Exist pptx file

Joni

Try the eoffice:

install.packages("eoffice")
library(eoffice)
gg <- ggplot(mtcars, aes(x = mpg , y = wt, colour = qsec)) + geom_point() + theme_minimal()
topptx(gg,file="gg.pptx")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM