简体   繁体   English

使用官员导出到 .pptx 时,绘图被压扁

[英]Plots are squished when exporting to .pptx using officer

I'm using the officer package to write ggplots to Powerpoint, and even though my plots look good in my Rstudio viewer, my plots are getting "squished" when I add them to any custom slide with smaller content boxes.我正在使用officer package 将 ggplots 写入 Powerpoint,即使我的情节在我的 Rstudio 查看器中看起来不错,但当我将它们添加到任何具有较小内容框的自定义幻灯片时,我的情节会变得“被压扁”。

I did my best to reproduce my issue with the following code:我尽力使用以下代码重现我的问题:

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")

Where template.pptx is a copy of the officer template with the "Title and Content" slide duplicated (and named "test_example") and content box shrunken down a little bit (availablehere for reproducibility).其中template.pptx官员模板的副本,其中复制了“标题和内容”幻灯片(并命名为“test_example”)并且内容框缩小了一点(可在此处获得可重复性)。

The first slide looks good, like this:第一张幻灯片看起来不错,像这样: 在此处输入图像描述 , but the second slide has the dimensions of the plot shrunken while the content in the plot stays the same size, giving it a "squished" feel: ,但第二张幻灯片的 plot 的尺寸缩小了,而 plot 中的内容保持不变,给人一种“压扁”的感觉: 在此处输入图像描述

Has anyone run into this before?有没有人遇到过这个? Are there any tricks/hacks out there that can tell my plot to decrease the size of all it's elements when the total image space is decreased?当总图像空间减少时,是否有任何技巧/黑客可以告诉我的 plot 减小所有元素的大小? I'd like to maintain the relative size of the elements of the plot as I move to different slide templates.当我移动到不同的幻灯片模板时,我想保持 plot 元素的相对大小。

If you're still here - THANK YOU FOR READING SO FAR: I'd appreciate any and all tips :)如果您还在这里 - 感谢您到目前为止的阅读:我将不胜感激任何和所有提示 :)

Here is the code that would avoid that problem with officer version 0.3.14 or later (I haven't update my package for a while and officer is an on-going package)这是可以避免officer版本0.3.14或更高版本出现该问题的代码(我有一段时间没有更新我的 package 并且官员是一个持续的软件包)

For better quality plot with smaller size using vector graphic instead.为了获得更好的质量 plot 尺寸更小,请改用矢量图形。 Using package rvg使用 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"))

I would recommend this when working with Powerpoint otherwise, you need to adjust template in Powerpoint and remember the order of each content container in that slide我建议在使用 Powerpoint 时这样做,否则,您需要在 Powerpoint 中调整模板并记住该幻灯片中每个内容容器的顺序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM