【问题标题】:How to save Pie chart into ppt slides using R?如何使用 R 将饼图保存到 ppt 幻灯片中?
【发布时间】:2022-01-19 10:48:00
【问题描述】:

我想将我的 R 饼图保存到 pptx,所以我设法到达了代码的最后一步。

df <- data.frame(Type = c('x','y'),Count = c(177,41))
ggplt <- ggplot(df, aes(x = "", y = Count, fill = Type)) +
         geom_col(width = 1) +
         scale_fill_manual(values = c("red", "yellow")) +
         coord_polar("y") + 
         geom_text(aes(label = paste0("Count=",Count)), position = position_stack(vjust = 0.5))+
         labs(title = "Employees Count")

myppt <- read_pptx()
myppt <-  add_slide(myppt,layout="Two Content",master = "Office Theme")
myppt <- ph_with(myppt,type = "body",value = ggplt,location = ph_location_left()) 

在运行ph_with() 的最后一条语句时,我收到错误消息:

Error in match.arg(type) : 
  'arg' should be one of “windows”, “cairo”, “cairo-png” 

如果有人能告诉我在哪里搞砸了,我将不胜感激!

【问题讨论】:

    标签: r ggplot2 data-visualization powerpoint


    【解决方案1】:

    你应该看看rmarkdown。有了它,您可以编织(转换)您的 r 文件或报告为 pptx 文件。要访问它,您可以在 rstudio 中单击新文件,然后按新的降价文件。

    【讨论】:

    • 虽然这是一个建议,但它并不是对 OP 问题的真正答案。
    【解决方案2】:

    问题是您在ph_with 中使用了type="body",但它没有type 参数。因此type 参数通过... 传递给png() 函数,这会导致神秘错误。

    也就是说,你可以通过删除type="body"来达到你想要的结果:

    library(officer)
    library(ggplot2)
    
    df <- data.frame(Type = c('x','y'),Count = c(177,41))
    ggplt <- ggplot(df, aes(x = "", y = Count, fill = Type)) +
      geom_col(width = 1) +
      scale_fill_manual(values = c("red", "yellow")) +
      coord_polar("y") + 
      geom_text(aes(label = paste0("Count=",Count)), position = position_stack(vjust = 0.5))+
      labs(title = "Employees Count")
    
    myppt <- read_pptx()
    myppt <-  add_slide(myppt,layout="Two Content",master = "Office Theme")
    myppt <- ph_with(myppt, value = ggplt, location = ph_location_left()) 
    
    print(myppt, "foo.pptx")
    

    【讨论】:

    • 哦!我明白了.. 谢谢@stefan。
    猜你喜欢
    • 2011-02-27
    • 2023-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    相关资源
    最近更新 更多