代码之家  ›  专栏  ›  技术社区  ›  Ben G

从Rstudio导出透明ggplot到PowerPoint有更好的解决方法吗?

  •  2
  • Ben G  · 技术社区  · 8 年前

    因此,我需要将我在R/Rstudio中制作的绘图复制并粘贴到PowerPoint中(不幸的是,我的政府客户要求所有内容都在PowerPoint中,否则这个问题只需将我的图形放在一个Rmarkdown文件中就可以轻松解决)。直到最近,我才发现透明对象不能复制并粘贴到PowerPoint中,至少在我一直使用的方法中是这样的(导出>复制为图元文件>复制打印>粘贴)。取而代之的是,在透明物体出现的地方,现在什么都没有了。我做了一些阅读,发现这与我的“图形引擎”不支持透明对象有关,“cairo”引擎可以,但我不明白这意味着什么,也不知道它是否是一个选项,我可以在Rstudio或PowerPoint中更改。

    下面是一些示例代码:

    iris %>%
    mutate(small_petal = if_else(Petal.Width > 1.5, "big", "small")) %>%
    ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Species, alpha = small_petal)) +
    geom_point()
    

    enter image description here

    为了得到我想要的颜色,我尝试了一个非常奢侈的变通方法。我制作了自定义的RGB值,这些值是不透明的,以模仿我想要的透明度。然后我使用 mutate 有四个 if_else s(每个alpha颜色组合一种颜色)。然后我用 scale_fill_identity 以应用我的自定义颜色,但它会生成一个带有我不想要的元素的图例。我需要使用以下命令使图例至少与我想要的某些元素一起显示:

    scale_fill_identity(guide = "legend",
                        labels = c("DUMMY LABEL",
                                   "DUMMY LABEL",
                                   "Small Petal",
                                   "Big Petal"),
                        breaks = unique(iris$custom_colors))
    

    然后删除PowerPoint中不需要的图例对象。任何帮助都非常感谢。

    3 回复  |  直到 8 年前
        1
  •  1
  •   Tom Wenseleers    7 年前

    如果要将R图导出到Powerpoint,还可以使用包装包 export 建立在 officer 刚从克兰身上出来的,看 https://cran.r-project.org/web/packages/export/index.html 以及演示 https://github.com/tomwenseleers/export

    典型的语法非常简单,输出采用本地Powerpoint矢量格式,完全支持半透明:

    install.packages("export")
    library(export)
    library(ggplot2)
    qplot(Sepal.Length, Petal.Length, data = iris, color = Species, 
          size = Petal.Width, alpha = I(0.7))     
    graph2ppt(file="ggplot2_plot.pptx", width=6, height=5) 
    

    您还可以使用它导出到Word、Excel、Latex或HTML,还可以使用它导出各种R stats对象的统计输出。

        2
  •  5
  •   JD Long    8 年前

    你有没有试过直接从RMarkdown渲染到PowerPoint?添加了该功能 pretty recently 您必须同时安装RMarkdown和RStudio的开发版本才能获得它。上一链接中的说明,但tldr;如下所示:

    1. 最新标记: devtools::install_github("rstudio/rmarkdown")
    2. 安装最新的RStudio预览: https://www.rstudio.com/products/rstudio/download/preview/

    当我在我的Mac电脑上试用时,我觉得输出效果很好:

    enter image description here

    我的RMarkdown测试如下:

    ---
    title: "testing ppt"
    author: "JD Long"
    date: "8/3/2018"
    output: powerpoint_presentation
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = FALSE)
    ```
    
    ## Test chunk
    
    ```{r iris, echo = TRUE}
    library(tidyverse)
    iris %>%
      mutate(small_petal = if_else(Petal.Width > 1.5, "big", "small")) %>%
      ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Species, alpha = small_petal)) +
      geom_point()
    ```
    
        3
  •  3
  •   Dieter Menne    8 年前

    直接渲染@JD Long提到的可能是一种方法,但是作为一种替代方法,Cairo可能会工作:

    p = iris %>%
      mutate(small_petal = if_else(Petal.Width > 1.5, "big", "small")) %>%
      ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Species, alpha = small_petal)) +
      geom_point() +
      theme_bw()   
    
    ggsave(p, filename = "a.png", type = "cairo")
    

    我还添加了theme_bw(),在白色背景下透明度看起来更好

    推荐文章