[英]How to increase the resolution of ggplots in Rmarkdown html files
[英]How to increase resolution of ggplots when exporting using officer R
我想将图表导出到 PPT,并且正在使用官员包来实现相同的功能。 然而,图表的默认分辨率很低,我想改变它。 我目前正在使用以下电话
ph_with_gg(p1,type = "chart",res = 1200)
其中 p1 是一个 ggplot 对象。 运行此程序时,我收到以下错误:
Error in png(filename = file, width = width, height = height, units =
"in", :
formal argument "res" matched by multiple actual arguments
真的很感激这方面的帮助
而不是使用png
,对于 PPT 中的高分辨率图,您应该使用矢量图形。
请参阅扩展名下:
带有
rvg
包的矢量图形rvg包带来了一个 API 来生成漂亮的矢量图形,这些矢量图形可以嵌入到 PowerPoint 文档或带有
officer
Excel 工作簿中。
这个包提供了函数dml()
和ph_with()
对应的方法来将 ggplots 导出为 .pptx 作为矢量图形。
示例:
library(ggplot2)
library(officer)
library(rvg)
library(magrittr)
data(iris)
read_pptx() %>%
add_slide(layout='Title and Content',master='Office Theme') %>%
ph_with('Iris Sepal Dimensions', location = ph_location_type(type="title")) %>%
ph_with(dml( ggobj=
ggplot(iris, aes(x=Sepal.Length,y=Sepal.Width,col=Species)) +
geom_point()), location = ph_location_type(type="body")) %>%
print('iris_presentation.pptx')
作为额外的好处,您将能够在 PowerPoint 中编辑图表。 例如,如果您决定将 3 个物种的名称大写,您可以只编辑图表而不是编辑数据和重新生成幻灯片。 (您也可以使绘图不可编辑,但默认设置为可编辑。)
将绘图保存到代码中的演示文稿中是否重要?
否则使用:
ggsave(filename = file, p1, width = width, height = height, dpi = dpi)
会给你一个你需要的任何分辨率的png..
(前提是文件名以 .png 结尾,并且您将宽度、高度和 dpi 设置为适当的值)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.