我正在使用officer包将图写入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是officer template的副本,其中“标题和内容”幻灯片被复制(并命名为"test_example"),内容框缩小了一点(可用here进行重现)。
第一张幻灯片看起来不错,如下所示:

,但第二张幻灯片缩小了情节的尺寸,而情节中的内容保持不变,给人一种“挤压”的感觉:

以前有没有人遇到过这种情况?有没有什么技巧/技巧可以告诉我的图,当总的图像空间减少时,减少它所有元素的大小?当我移动到不同的幻灯片模板时,我希望保持绘图元素的相对大小。
如果你还在这里--感谢你到目前为止的阅读!我将非常感谢任何和所有的提示:)
发布于 2021-02-23 09:26:39
以下是在officer版本0.3.14或更高版本中避免该问题的代码(我已经有一段时间没有更新我的包了,而officer是一个正在进行的包)
为获得更高质量的绘图,请改用矢量图形。使用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中调整模板,并记住幻灯片中每个内容容器的顺序
https://stackoverflow.com/questions/66262545
复制相似问题