首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将purrr和cowplot相结合形成一个绘图网格

将purrr和cowplot相结合形成一个绘图网格
EN

Stack Overflow用户
提问于 2018-03-12 17:25:08
回答 1查看 1.1K关注 0票数 2

我已经使用ggplot2创建了一个purrr图形列表列,现在我想使用cowplot::plot_grid()将它们组合成一个单独的图。我该怎么做?有一种蛮力的方法可以做到这一点,但是当我不知道列表列中有多少元素时,这种方法可能就行不通了。

代码语言:javascript
复制
### libraries needed
library(tidyverse)
# install.packages("devtools")
devtools::install_github("IndrajeetPatil/ggstatsplot")

### creating list column with plots
plots <- datasets::mtcars %>%
  dplyr::mutate(.data = ., cyl2 = cyl) %>%
  dplyr::group_by(.data = ., cyl) %>%
  tidyr::nest(data = .) %>%
  dplyr::mutate(
    .data = .,
    plot = data %>%
      purrr::map(
        .x = .,
        .f = ~ ggstatsplot::ggbetweenstats(
          data = .,
          x = am,
          y = mpg,
          title = as.character(.$cyl2)
        )
      )
  )
#> Warning:  aesthetic `x` was not a factor; converting it to factorReference:  Welch's t-test is used as a default. (Delacre, Lakens, & Leys, International Review of Social Psychology, 2017).Note:  Bartlett's test for homogeneity of variances: p-value =  0.317Warning:  aesthetic `x` was not a factor; converting it to factorReference:  Welch's t-test is used as a default. (Delacre, Lakens, & Leys, International Review of Social Psychology, 2017).Note:  Bartlett's test for homogeneity of variances: p-value =  0.144Warning:  aesthetic `x` was not a factor; converting it to factorReference:  Welch's t-test is used as a default. (Delacre, Lakens, & Leys, International Review of Social Psychology, 2017).Note:  Bartlett's test for homogeneity of variances: p-value =  0.201

### creating a grid with cowplot

# brute force way to do this would be
# this works fine with 3 plots, but I might have way more plots than that
cowplot::plot_grid(plots$plot[[1]],
                   plots$plot[[2]],
                   plots$plot[[3]],
                   nrow = 3,
                   ncol = 1, 
                   labels = c("(a)","(b)","(c)"))

代码语言:javascript
复制
# searching for a more tidy and elegant way to do this
# my attempted code
cowplot::plot_grid(plotlist = list(plots$plot),
                     nrow = 3,
                     ncol = 1, 
                     labels = c("(a)","(b)","(c)"))
#> Error in plot_to_gtable(x): Argument needs to be of class "ggplot", "gtable", "grob", "recordedplot", or a function that plots to an R graphicsdevice when called, but is a list

reprex封装创建于2018-03-12 (v0.2.0)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-12 18:50:02

plots$plot已经是一个情节列表,所以您所需要做的就是调用

代码语言:javascript
复制
cowplot::plot_grid(plotlist = plots$plot)
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49241141

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档