在具有给定大小的功率点幻灯片中绘制ggplot2图形 - R.

问题描述 投票:1回答:2

我试图用ggplot2包在功率点幻灯片上绘制officer图。我实际上可以这样做(直接在ppt中打印ggplot2),但是因为我需要增加ggplot2图的大小(对于ppt幻灯片),我已经理解ggplot2图依赖于窗口的大小(在RStudio)或你设置的任何东西,就好像你要导出它一样,我正在寻找一种方法来(1)导出具有给定大小的ggplot2图形(例如:height=5, width=8),(2)从ppt导入/读取码:

library(officer)
library(devEMF)
library(magrittr)
library(ggplot2)

t <- "../example.pptx"
filename <- gg

read_pptx() %>% 
  add_slide(layout = "Title and Content", master = "Office Theme") %>% 
  ph_with_img(src = filename, width = 6, height = 4, type = "body") %>% 
  print(target = t)

gg是来自ggplot2的任何情节(实际上并不重要)。 t是输出文件地址。

ph_with_img

PowerPoint documents and graphics

PD:如果有一些我不知道的包/命令而且我仍然找不到,我可以编辑ggplot2的大小,所有这些都是不必要的。

r ggplot2 powerpoint officer
2个回答
2
投票

我成功地首先将ggplot2图保存为.png,然后将该文件调用到ph_with_img。有点迂回,但它的工作原理。你也可以将图形保存为?tempfile然后?unlink,但我有点喜欢我的图形文件夹。

ggplot() +
  (code for my ggplot)

ggsave("../thisplot.png", width = 6, height = 4)

read_pptx() %>% 
  add_slide(layout = "Title and Content", master = "Office Theme") %>% 
  ph_with_img(src = "../thisplot.png", width = 6, height = 4, type = "body") %>% 
  print(target = t)

2
投票

我刚刚在export上创建了一个新的包officer,它可以很容易地使用命令graph2ppt()执行此操作,并且可以很好地导出矢量格式,而不是上面发布的其他答案中的位图,例如

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) 
© www.soinside.com 2019 - 2024. All rights reserved.