使用officer导出到.pptx时,打印会被压扁

使用officer导出到.pptx时,打印会被压扁,r,powerpoint,officer,R,Powerpoint,Officer,我正在使用officer软件包将ggplots写入Powerpoint,尽管我的绘图在我的Rstudio查看器中看起来不错,但当我将它们添加到任何带有较小内容框的自定义幻灯片时,我的绘图都会被“压扁” 我尽力用以下代码重现我的问题: library(tidyverse) library(officer) p <- data.frame(x = rnorm(100)) %>% ggplot() + geom_density(aes(x)) + labs(title = &

我正在使用
officer
软件包将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")
库(tidyverse)
图书馆(主任)
p%
ggplot()+
几何密度(aes(x))+
实验室(title=“这是一个标题”)+
注释(“文本”,x=0,y=0.2,label=“这是一些文本”)
读取\u pptx(“~/Downloads/template.pptx”)%>%
添加幻灯片(layout=“Title and Content”)%%>%
ph值与(p,位置=ph值位置类型(“主体”,id=1))%>%
添加幻灯片(layout=“text\u示例”)%%>%
ph值与(p,位置=ph值位置类型(“主体”,id=1))%>%
打印(“~/Desktop/test\u example.pptx”)
其中,
template.pptx
是“标题和内容”幻灯片的副本(并命名为“测试示例”),内容框缩小了一点(可用于再现性)

第一张幻灯片看起来不错,如下所示:,但第二张幻灯片缩小了绘图的尺寸,而绘图中的内容保持不变,给人一种“挤压”的感觉:

以前有人碰到过这个吗?有没有什么技巧/技巧可以告诉我的绘图在总图像空间减少时减少所有元素的大小?我希望在移动到不同的幻灯片模板时保持绘图元素的相对大小


如果你还在这里-谢谢你阅读到目前为止!我非常感谢所有的提示:)

以下是避免
officer
版本
0.3.14
或更高版本出现问题的代码(我已经有一段时间没有更新我的软件包了,officer是一个持续的软件包)

为了获得更好的绘图质量,使用矢量图形代替较小的尺寸。使用包
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"))
库(rvg)
图书馆(gridExtra)
文件%%
添加幻灯片(layout=“text\u示例”)%%>%
#在相对于左上角1英寸的位置添加绘图
#尺寸为6平方英寸
#grid.arrange只是一个用于生成绘图的代码,实际上并不安排任何内容
ph值为(值=dml(代码=grid.arrange(plot),bg=“transparent”),
位置=PHU位置(左=1,顶=1,宽=6,
高度=6,bg=“透明”))%>%
#在幻灯片右侧添加一个带有特定
#尺寸和颜色
带有(值=块列表(fpar(ftext)(“”))的PHU,
位置=PHU位置(左=6.11,顶=1,宽=3.73,
高度=3.3,bg=“#C6D9F1”))

我建议在使用Powerpoint时使用此选项,否则,您需要调整Powerpoint中的模板,并记住该幻灯片中每个内容容器的顺序

这是由于您使用
add\u slide(layout=“text\u example”)
的布局,该布局可能会将某些内容容器设置为该大小。是的,我理解,但是,当我将绘图元素移动到幻灯片中不同大小的容器中时,有没有办法保持绘图元素的相对大小?不幸的是,这并不能解决问题-当框变小时,绘图仍然会被压扁。我误解了你的观点-如果你想要更小的图形质量更好,那么就改用矢量图形。-使用
rvg
officer
调整答案,如果您想调整字体大小等。。。它需要在不同的模板中手动调整。