簡體   English   中英

使用官員 R 導出時如何提高 ggplots 的分辨率

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM