【问题标题】:How to increase resolution of ggplots when exporting using officer R使用officer R导出时如何提高ggplots的分辨率
【发布时间】:2018-08-20 19:28:29
【问题描述】:

我想将图表导出为 PPT,并使用officer 包来实现。但是,图表的默认分辨率很低,我想改变它。我目前正在使用以下调用

    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

非常感谢有关此方面的帮助

【问题讨论】:

    标签: r ggplot2 officer


    【解决方案1】:

    对于 PPT 中的高分辨率绘图,您应该使用 矢量图形,而不是使用 png

    extensions

    带有包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 个物种的名称大写,您可以只编辑图表,而不是编辑数据并重新生成幻灯片。 (您也可以使绘图不可编辑,但可编辑是默认设置。)

    【讨论】:

    • 感谢您指向这个包。它有一个名为 ph_with_vg 的函数来解决这个问题。非常感谢
    【解决方案2】:

    将绘图保存到代码中的演示文稿中是否重要?

    否则使用:

    ggsave(filename = file, p1, width = width, height = height, dpi = dpi)
    

    会给你一个你需要的任何分辨率的 png..

    (前提是文件名以 .png 结尾,并且您将宽度、高度和 dpi 设置为适当的值)

    【讨论】:

    • 嗨。你怎么知道什么值适合设置 dpi?
    猜你喜欢
    • 2020-10-05
    • 2015-04-19
    • 1970-01-01
    • 1970-01-01
    • 2020-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-19
    相关资源
    最近更新 更多