R上的ph_with_vg和ggplot

时间:2017-11-03 10:23:33

标签: r ggplot2 officer

我可以使用以下代码使用ggplot创建我想要的图表:

    ggplot(data, aes(x=as.Date(data$Date, "%d/%m/%Y"), y=items)) + geom_col(fill="#00cccc")

然而,当我使用我的完整代码时,我收到的错误是" StartTag:无效的元素名称[68]"

    my_pres<-
      # Load template
      read_pptx("C:/Users/USERNAME/Desktop/template.pptx") %>%
      # 02 - SLIDE
      add_slide(layout="Title with Subtitle and Content", master="MySlides2016") %>%
      # 02 - Title
      ph_with_text(type = "title", str = "Items by Day") %>%
      # 02 - Chart
      ph_with_vg_at(code = ggplot(data, aes(x=as.Date(data$Date, "%d/%m/%Y"), y=items)) + geom_col(fill="#00cccc"),left = 1, top = 2, width = 6, height = 4)

1 个答案:

答案 0 :(得分:3)

如果您阅读了可用文档here,则表示您必须在函数print(your_ggplot_object)中使用ph_with_vg_at,以便执行以下操作:

library("officer")
library("rvg")
library("magrittr")
library("ggplot2")

gg <- ggplot(mtcars, aes(x = mpg , y = wt, colour = qsec)) + geom_point() + theme_minimal()

my_pres<-
  # Load template
  read_pptx() %>%
  # 02 - SLIDE
  add_slide(layout = "Title and Content", master = "Office Theme") %>%
  # 02 - Title
  ph_with_text(type = "title", str = "Items by Day") %>%
  # 02 - Chart
  ph_with_vg_at(code = print(gg),left = 1, top = 2, width = 6, height = 4)

# Save
tmp <- tempfile(fileext = ".pptx")
print(my_pres, target = tmp)

# Open
browseURL(tmp)