我试图用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
是输出文件地址。
PowerPoint documents and graphics
PD:如果有一些我不知道的包/命令而且我仍然找不到,我可以编辑ggplot2的大小,所有这些都是不必要的。
我成功地首先将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)
我刚刚在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)