如何在使用高级职员R导出时提高ggplots的分辨率 [英] How to increase resolution of ggplots when exporting using officer R

查看:577
本文介绍了如何在使用高级职员R导出时提高ggplots的分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将图表导出到PPT,并且正在使用Officer包来实现此目的.但是,图表的默认分辨率较低,我想对此进行更改.我目前正在使用以下电话

I want to export charts to a PPT and am using the officer package to achieve the same. However, the default resolution of the charts is low and I would like to change that. I am currently using the following call

    ph_with_gg(p1,type = "chart",res = 1200)

其中p1是ggplot对象.运行此命令时,出现以下错误:

where p1 is a ggplot object. On running this, I get the following error:

    Error in png(filename = file, width = width, height = height, units = 
"in",  : 
  formal argument "res" matched by multiple actual arguments

真的很感谢这方面的帮助

Would really appreciate the help around this

推荐答案

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

Rather than using png, for high-resolution plots in PPT you should be using vector graphics.

请参见扩展名:

带有软件包rvg

Vector graphics with package rvg

软件包 rvg 将API引入了 产生可以嵌入到PowerPoint中的精美矢量图形 带有officer的文档或Excel工作簿.

The package rvg brings an API to produce nice vector graphics that can be embedded in PowerPoint documents or Excel workbooks with officer.

此软件包提供函数dml()ph_with()对应的方法,将ggplots导出为.pptx作为矢量图形.

This package provides functions dml() and ph_with() corresponding method to export ggplots to .pptx as vector graphics.

示例:

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

As an additional benefit, you will be able to edit the charts in PowerPoint. For example, if you decided to capitalize the names of the 3 species, you could just edit the chart instead of editing the data and regenerating the slides. (You can also make the plots non-editable, but editable is the default.)

这篇关于如何在使用高级职员R导出时提高ggplots的分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆