Home > Software engineering >  How to save Pie chart into ppt slides using R?
How to save Pie chart into ppt slides using R?

Time:12-16

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 !!

CodePudding user response:

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

enter image description here

CodePudding user response:

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.

  • Related