使用官员R导出时如何提高ggplots的分辨率

Nis*_*awa 1 r ggplot2 officer

我想将图表导出到PPT,并使用官方包来实现相同的目标.但是,图表的默认分辨率很低,我想改变它.我目前正在使用以下电话

    ph_with_gg(p1,type = "chart",res = 1200)
Run Code Online (Sandbox Code Playgroud)

其中p1是ggplot对象.运行此时,我收到以下错误:

    Error in png(filename = file, width = width, height = height, units = 
"in",  : 
  formal argument "res" matched by multiple actual arguments
Run Code Online (Sandbox Code Playgroud)

非常感谢周围的帮助

C8H*_*4O2 6

而不是使用pngPPT中的高分辨率图,你应该使用矢量图形.

查看扩展名:

矢量图形与包 rvg

软件包rvg带来了一个API,可以生成漂亮的矢量图形,可以嵌入到PowerPoint文档或Excel工作簿中officer.

此包提供函数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')
Run Code Online (Sandbox Code Playgroud)

作为额外的好处,您将能够在PowerPoint中编辑图表.例如,如果您决定将3种物种的名称大写,则只需编辑图表而不是编辑数据并重新生成幻灯片.(您也可以使图表不可编辑,但可编辑是默认值.)