ggplot2的默认面板布局::facet_wrap()?

ggplot2的默认面板布局::facet_wrap()?,r,ggplot2,facet-wrap,R,Ggplot2,Facet Wrap,我试图了解ggplot2::facet\u wrap()的默认行为,即面板布局是如何随着facet数量的增加而决定的 我已经阅读了?facet\u wrap帮助文件,也用谷歌搜索了这个主题,但效果有限。在其中一个例子中,facet\u wrap()被称为“返回一个对称的绘图矩阵”,但我没有找到任何东西来解释默认行为的确切含义 所以接下来我做了一系列的图,图中有越来越多的面(代码显示在下面) 图像中的图案使其看起来像是facet\u wrap()尝试“制作正方形” 问题 对吗?facet\u w

我试图了解
ggplot2::facet\u wrap()
的默认行为,即面板布局是如何随着facet数量的增加而决定的

我已经阅读了
?facet\u wrap
帮助文件,也用谷歌搜索了这个主题,但效果有限。在其中一个例子中,
facet\u wrap()
被称为“返回一个对称的绘图矩阵”,但我没有找到任何东西来解释默认行为的确切含义

所以接下来我做了一系列的图,图中有越来越多的面(代码显示在下面)

图像中的图案使其看起来像是
facet\u wrap()
尝试“制作正方形”

问题

  • 对吗?
    facet\u wrap
    是否尝试渲染facet 所以从整体上看,它们最像一个正方形 行和列中的元素数
  • 如果没有,它实际上在做什么?图形参数是否包含在内
  • 生成绘图的代码

    # load libraries
    library(ggplot2)
    library(ggpubr)
    
    # plotting function
    facetPlots <- function(facets, groups = 8){
       # sample data  
       df <- data.frame(Group = sample(LETTERS[1:groups], 1000, replace = T),
                        Value = sample(1:10000, 1000, replace = T),
                        Facet = factor(sample(1:facets, 1000, replace = T)))
       # get means
       df <- aggregate(list(Value = df$Value), 
                       list(Group = df$Group, Facet = df$Facet), mean)
    
       # plot
       p1 <- ggplot(df, aes(x= Group, y= Value, fill = Group))+
               geom_bar(stat="identity", show.legend = FALSE)+
               facet_wrap(. ~ Facet) +
               theme_bw()+
         theme(strip.text.x = element_text(size = 6, 
            margin = margin(.1, 0, .1, 0, "cm")),
           axis.text.x=element_blank(),
           axis.ticks=element_blank(),
           axis.title.x=element_blank(),
           axis.text.y=element_blank(),
           axis.title.y=element_blank(),
           plot.margin = unit(c(3,3,3,3), "pt"))
      p1
    
    }
    
    # apply function to list
    plot_list <- lapply(c(1:25), facetPlots)
    # unify into single plot
    plot <- ggpubr::ggarrange(plotlist = plot_list)  
    
    #加载库
    图书馆(GG2)
    图书馆(ggpubr)
    #绘图功能
    
    facetPlots以下是如何计算默认行数和列数:

    ncol <- ceiling(sqrt(n))
    nrow <- ceiling(n/ncol)
    
    以下是计算的行/列数:

    #   n   ncol   nrow
    #   1      1      1
    #   2      2      1
    #   3      2      2
    #   4      2      2
    #   5      3      2
    #   6      3      2
    #   7      3      3
    #   8      3      3
    #   9      3      3
    #  10      4      3
    #  11      4      3
    #  12      4      3
    #  13      4      4
    #  14      4      4
    #  15      4      4
    #  16      4      4
    #  17      5      4
    #  18      5      4
    #  19      5      4
    #  20      5      4
    #  21      5      5
    #  22      5      5
    #  23      5      5
    #  24      5      5
    #  25      5      5
    

    不看ggplot代码,如果它使用类似于
    grDevices::n2mflow
    的东西作为默认值,我也不会感到惊讶(尽管对于某些绘图,它似乎是从行而不是列切换过来的)。。。也许
    #   n   ncol   nrow
    #   1      1      1
    #   2      2      1
    #   3      2      2
    #   4      2      2
    #   5      3      2
    #   6      3      2
    #   7      3      3
    #   8      3      3
    #   9      3      3
    #  10      4      3
    #  11      4      3
    #  12      4      3
    #  13      4      4
    #  14      4      4
    #  15      4      4
    #  16      4      4
    #  17      5      4
    #  18      5      4
    #  19      5      4
    #  20      5      4
    #  21      5      5
    #  22      5      5
    #  23      5      5
    #  24      5      5
    #  25      5      5