与军官一起从GGPLOT出口多句话

发布于 2025-02-13 02:03:51 字数 860 浏览 1 评论 0原文

不知何故,我无法使用官员软件包将包含三个子图的绘图正确导出。我将最多的MWE具有相同但不同的数据产生我要导出的绘图,

library(fpp3)
library(officer)
library(rvg)


p1 <- global_economy %>%
  filter(Code == "CAF") %>%
  gg_tsdisplay(difference(Exports), plot_type='partial')


#PPT
p_dml <- rvg::dml(ggobj = p1, editable = F)

my_pres <- read_pptx("...path/presentation.pptx")
my_pres <- add_slide(my_pres,layout = "Headline 1-zeilig", master = "Master-Design") #should be adjusted 
my_pres<- ph_with(my_pres, value = p_dml , location = ph_location_fullsize())

print(my_pres, target = "...path/presentation.pptx") 

中产生的图表。

这是我在r: “输入图像在这里说明“

,但在最终的PowerPoint中,只显示右下图,而不是所有三个图。

somehow I am not able to properly export a plot containing three subplots into my PowerPoint with the officer package. I will most an MWE with the same but different data that produces the plot that I want to export

library(fpp3)
library(officer)
library(rvg)


p1 <- global_economy %>%
  filter(Code == "CAF") %>%
  gg_tsdisplay(difference(Exports), plot_type='partial')


#PPT
p_dml <- rvg::dml(ggobj = p1, editable = F)

my_pres <- read_pptx("...path/presentation.pptx")
my_pres <- add_slide(my_pres,layout = "Headline 1-zeilig", master = "Master-Design") #should be adjusted 
my_pres<- ph_with(my_pres, value = p_dml , location = ph_location_fullsize())

print(my_pres, target = "...path/presentation.pptx") 

This is the graph that I am producing inside R:

enter image description here

But in the final PowerPoint only the lower right figure is displayed and not all three graphs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长不大的小祸害 2025-02-20 02:03:52

问题在于,gg_tsdisplay返回的对象不是ggplot对象,而是ggplot对象列表。结果,仅此列表的最后一个元素被导出到PPTX,或者在第一个转换为DML对象的情况下,您会遇到错误。

一种可能的修复方法是使用patchwork软件包构建多图,作为副作用将“将”绘图列表“转换”到GGPLOT对象。这样做之后,无论是ggplot对象还是dml对象,您都可以轻松地导出到PPTX。在下面的代码中,我使用patchwork :: wrap_plots并使用design参数来模仿您的多图的布局:

library(fpp3)
library(officer)
library(rvg)

p1 <- global_economy %>%
  filter(Code == "CAF") %>%
  gg_tsdisplay(difference(Exports), plot_type='partial')

library(patchwork)

p1 <- p1 |>
  wrap_plots(design = "AA\nBC")

p_dml <- rvg::dml(ggobj = p1, editable = F)

my_pres <- read_pptx()
my_pres <- add_slide(my_pres,layout = "Title and Content", master = "Office Theme")
my_pres<- ph_with(my_pres, value = p_dml, location = ph_location_fullsize())

print(my_pres, target = "presentation.pptx")

“在此处输入图像说明”

The issue is that the object returned by gg_tsdisplay is not a ggplot object but a list of ggplot objects instead. As a consequence only the last element of this list is exported to the pptx or you get an error in the case where your first convert to a dml object.

One possible fix would be to build your multi plot using the patchwork package which as a side effect will "convert" the list of plots to a ggplot object. After doing so you could easily export to pptx whether as a ggplot object or as an dml object. In my code below I use patchwork::wrap_plots and use the design argument to mimic the layout of your multi plot:

library(fpp3)
library(officer)
library(rvg)

p1 <- global_economy %>%
  filter(Code == "CAF") %>%
  gg_tsdisplay(difference(Exports), plot_type='partial')

library(patchwork)

p1 <- p1 |>
  wrap_plots(design = "AA\nBC")

p_dml <- rvg::dml(ggobj = p1, editable = F)

my_pres <- read_pptx()
my_pres <- add_slide(my_pres,layout = "Title and Content", master = "Office Theme")
my_pres<- ph_with(my_pres, value = p_dml, location = ph_location_fullsize())

print(my_pres, target = "presentation.pptx")

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文