简体   繁体   中英

How to save Pie chart into ppt slides using R?

I want to save my R pie plot to pptx so I managed to reach the last step of code.

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

while running the last statement of ph_with() I am getting error saying:

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

I would appreciate if someone could tell where I am messing up the things !!

You should take a look at rmarkdown . With it you can knit (convert) you r file or report into a pptx file. To access this you klick new file in rstudio and than press new markdown file.

The issue is that you used type="body" in ph_with which however has no type argument. Therefore the type argument is passed on via ... to the png() function which gives rise to the mysterious error.

This said, you could achieve your desired result by removing 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")

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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