【问题标题】:ph_with_vg and ggplot on RR上的ph_with_vg和ggplot
【发布时间】:2018-04-16 01:06:08
【问题描述】:

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

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

但是,当我将其与完整代码一起使用时,我收到一条错误消息,显示为“StartTag:invalid element name [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)

【问题讨论】:

    标签: r ggplot2 officer


    【解决方案1】:

    如果您阅读了可用的文档here,它说您必须在函数ph_with_vg_at 中使用print(your_ggplot_object),所以您可以这样做:

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-25
      • 1970-01-01
      • 1970-01-01
      • 2022-07-29
      • 1970-01-01
      • 2014-11-24
      • 1970-01-01
      相关资源
      最近更新 更多