gpt4 book ai didi

R:将当前事件的 R 图导出到 Powerpoint/Word/LibreOffice 的功能

转载 作者:行者123 更新时间:2023-12-03 01:10:48 25 4
gpt4 key购买 nike

目前,我总是使用出色的 ReporteRs 包将 R 图形以矢量格式导出到 Powerpoint,如下所示

library(ReporteRs)
myplot = function() print(qplot(Sepal.Length, Petal.Length, data = iris, color = Species, size = Petal.Width, alpha = I(0.7)))
doc = pptx()
doc = addSlide(doc, slide.layout = "Blank")
doc = addPlot( doc, myplot, vector.graphic = TRUE, fontname="Arial",
offx = 0.1*dim(doc)$slide.dim["width"], offy = 0.05*dim(doc)$slide.dim["height"],
width = 0.8*dim(doc)$slide.dim["width"], height = 0.9*dim(doc)$slide.dim["height"])
writeDoc( doc, "plot.pptx")

enter image description here

我发现此工作流程中不方便的是我现在必须将绘图命令包装在一个函数中(使用 print() 表示 latticeggplot2 图,或者只是 return(plot()) 用于基本 R 图),而且我不喜欢手动指定偏移量和大小(我通常更喜欢只得到一个几乎页面填充,幻灯片中的居中图表)。我只是想知道是否无法先绘制图(基本 R 图、lattice 图或 ggplot2 图),然后定义函数 export2PPT 将当前图形设备的输出捕获为函数,并将其传递给 ReporteRsaddPlot 以将绘图导出为 Powerpoint(居中且适当缩放) ),类似于函数 dev.copy2pdf ,其工作方式与导出为 PDF 相同?

一旦我们有了这样的函数,甚至可能 modify the grDevices source code功能 windows() and savePlot()如果使用 windows() 设备,是否需要一个附加命令“文件...另存为 Powerpoint”?或者这是 R Core 人员要做的事情?

考虑到 MS Office/LibreOffice 的主导地位,我认为特别是在类里面使用,这将非常方便。(因为 Powerpoint 比 PDF 更容易编辑,允许人们轻松地对 R 图形的最终布局进行微小的更改,并且矢量输出至少与 PDF 一样好,而且还完全支持透明度 - 对于 PDF,我发现导入在 Inkscape 中通常有点错误,除非使用 Adob​​e Illustrator)

有人知道如何做到这一点吗?

编辑:与此同时,我找到了解决方案,并包装了导出函数,将当前事件的 R 图导出到 CRAN 包中的 powerpoint,请参阅 https://cran.r-project.org/web/packages/export/index.htmlhttps://github.com/tomwenseleers/export供演示使用。该包现在使用Officer包作为后端,因为ReporteRs已被弃用...

最佳答案

刚刚制作了一个新的 CRAN 包export,它允许将当前事件图形设备中的 R 图以可编辑 (DrawingML) 矢量格式导出到 Powerpoint 或 Word(或 LibreOffice Impress/Writer)作为一行,完全支持透明度等,请参阅 https://cran.r-project.org/web/packages/export/index.html并进行演示 https://github.com/tomwenseleers/export

例如:

install.packages("export")
library(export)

导出格子图的示例:

library(effects)
fit=lm(prestige ~ type + income*education, data=Prestige)
plot(Effect(c("income", "education"), fit),multiline=T, ci.style="bands")
graph2ppt(file="effect plot.pptx", width=7, height=5)

enter image description here

在 PPT 中右键单击“取消分组”后,可以看到很好的矢量格式:

enter image description here

通过使用文件...另存为...PDF,这些图表还可以从 Word 或 PPT 完美导出为(矢量格式)PDF,并且对布局进行细微调整比直接编辑要容易得多PDF。

如果 R Core 或 RStudio 用户希望包含此功能,请这样做 - 考虑到 Office 套件的主导地位和广泛使用,我认为在类里面使用会非常方便!

关于R:将当前事件的 R 图导出到 Powerpoint/Word/LibreOffice 的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31212659/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com