0

我希望能够在生成它们时选择插入到我的 powerpoint 中的图的尺寸。我意识到我可以简单地将它们保存为单独的文件,然后插入它们。但我更希望能够在创建时将它们插入幻灯片中进行操作。无论是在插入之前更改绘图尺寸还是更改幻灯片的边界框尺寸。

我已经做了一些测试:

ph_with 中的“use_loc_size = F”似乎只适用于图像,据我从我所做的测试中可以看出。

更改像素质量确实会在一定程度上改变比例,但是如果我触摸它,标签和图形似乎会以非常不同的方式发生变化(标签越大,像素数越高,而图形越小,反之亦然,当我减少像素数)

示例代码:

library(flextable)
library(rvg)
library(officer)
library(ggplot2)

path_out <- "."

# prep ggplot
p1 <- iris %>% 
  ggplot() +
  geom_point(aes(Sepal.Length,Petal.Length,color = Species), size = 3) +
  theme_minimal()

# prep editable graph (rvg)
p2 <- dml(ggobj = p1)

my_pres <- read_pptx() %>%
  #slide 1
  add_slide(layout = "Title and Content", master = "Office Theme") %>%
  ph_with(value = p1, location = ph_location_type("body", width = 2, height = 13)) %>%
  #slide 2
  add_slide() %>%
  ph_with(value = p2, location = ph_location_type("body"), width = 6, height = 6) %>%
  print(target = file.path(path_out,"example_v1.pptx"))
4

1 回答 1

0

如果您想在不使用占位符属性的情况下从 R 定义大小,您可以使用ph_location并指定宽度、高度和左上角位置:

library(flextable)
library(rvg)
library(officer)
library(ggplot2)
library(magrittr)

path_out <- "."

# prep ggplot
p1 <- iris %>% 
  ggplot() +
  geom_point(aes(Sepal.Length,Petal.Length,color = Species), size = 3) +
  theme_minimal()

# prep editable graph (rvg)
p2 <- dml(ggobj = p1)

my_pres <- read_pptx() %>%
  #slide 1
  add_slide(layout = "Title and Content", master = "Office Theme") %>%
  ph_with(value = p1, location = ph_location("body", left = 1, top = 1, width = 5, height = 5)) %>%
  #slide 2
  add_slide() %>%
  ph_with(value = p2, location = ph_location("body", left = 1, top = 1, width = 5, height = 5)) %>%
  print(target = file.path(path_out,"example_v1.pptx"))

在此处输入图像描述

于 2020-03-20T21:43:01.653 回答