代码之家  ›  专栏  ›  技术社区  ›  Chris

在给定大小的PowerPoint幻灯片中绘制ggplot2图-r

  •  0
  • Chris  · 技术社区  · 6 年前

    我想画一个 ggplot2 在Power Point幻灯片中使用 officer 包裹。实际上我能做到(打印 GGPROTT2 直接在PPT),但因为我需要增加 GGPROTT2 图表(PPT幻灯片),我明白 GGPROTT2 图形取决于窗口的大小(在 RStudio )或者,无论您将其设置为如何导出,我都在寻找一种方法:(1)导出具有给定大小的ggplot2图(例如: height=5, width=8 ),(2)从PPT代码导入/读取:

    library(officer)
    library(devEMF)
    library(magrittr)
    library(ggplot2)
    
    t <- "../example.pptx"
    filename <- gg
    
    read_pptx() %>% 
      add_slide(layout = "Title and Content", master = "Office Theme") %>% 
      ph_with_img(src = filename, width = 6, height = 4, type = "body") %>% 
      print(target = t)
    

    gg 是来自ggplot2的任何情节(实际上并不重要)。 t 是输出文件地址。

    ph_with_img

    PowerPoint documents and graphics

    PD:如果有一些包/命令我不知道,但我仍然找不到,在那里我可以编辑ggplot2的大小,那么所有这些都是不必要的。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Adam    6 年前

    我首先成功地挽救了 ggplot2 图形化为.png,然后将该文件调用到 ph_with_img . 有点迂回,但它起作用。您还可以将图形另存为 ?tempfile 然后 ?unlink 但是我有点喜欢我的图表的文件夹。

    ggplot() +
      (code for my ggplot)
    
    ggsave("../thisplot.png", width = 6, height = 4)
    
    read_pptx() %>% 
      add_slide(layout = "Title and Content", master = "Office Theme") %>% 
      ph_with_img(src = "../thisplot.png", width = 6, height = 4, type = "body") %>% 
      print(target = t)