[英]Unable to specify slide size when exporting a plot from R to a pptx file using R packages OFFICER and RVG
[英]Plots are squished when exporting to .pptx using officer
我正在使用officer
package 將 ggplots 寫入 Powerpoint,即使我的情節在我的 Rstudio 查看器中看起來不錯,但當我將它們添加到任何具有較小內容框的自定義幻燈片時,我的情節會變得“被壓扁”。
我盡力使用以下代碼重現我的問題:
library(tidyverse)
library(officer)
p <- data.frame(x = rnorm(100)) %>%
ggplot() +
geom_density(aes(x)) +
labs(title = "This is a title") +
annotate("text", x = 0, y = 0.2, label = "This is some text")
read_pptx("~/Downloads/template.pptx") %>%
add_slide(layout = "Title and Content") %>%
ph_with(p, location = ph_location_type("body", id = 1)) %>%
add_slide(layout = "text_example") %>%
ph_with(p, location = ph_location_type("body", id = 1)) %>%
print("~/Desktop/test_example.pptx")
其中template.pptx
是官員模板的副本,其中復制了“標題和內容”幻燈片(並命名為“test_example”)並且內容框縮小了一點(可在此處獲得可重復性)。
第一張幻燈片看起來不錯,像這樣: ,但第二張幻燈片的 plot 的尺寸縮小了,而 plot 中的內容保持不變,給人一種“壓扁”的感覺:
有沒有人遇到過這個? 當總圖像空間減少時,是否有任何技巧/黑客可以告訴我的 plot 減小所有元素的大小? 當我移動到不同的幻燈片模板時,我想保持 plot 元素的相對大小。
如果您還在這里 - 感謝您到目前為止的閱讀:我將不勝感激任何和所有提示 :)
這是可以避免officer
版本0.3.14
或更高版本出現該問題的代碼(我有一段時間沒有更新我的 package 並且官員是一個持續的軟件包)
為了獲得更好的質量 plot 尺寸更小,請改用矢量圖形。 使用 package rvg
library(rvg)
library(gridExtra)
document %<>%
add_slide(layout = "text_example") %>%
# add the plot in at position relative to 1 inches top left
# with size is 6 inches square
# grid.arrange is just a code for generate plot not actually arrange anything
ph_with(value = dml(code = grid.arrange(plot), bg = "transparent"),
location = ph_location(left=1, top=1, width=6,
height=6, bg="transparent")) %>%
# Add a text box on the right side of the slide with certain
# size & colors
ph_with(value = block_list(fpar(ftext(" "))),
location = ph_location(left=6.11, top=1, width=3.73,
height=3.3, bg="#C6D9F1"))
我建議在使用 Powerpoint 時這樣做,否則,您需要在 Powerpoint 中調整模板並記住該幻燈片中每個內容容器的順序
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.