ggplot2 :: facet_wrap()的默认面板布局? [英] Default panel layout of ggplot2::facet_wrap()?

查看:49
本文介绍了ggplot2 :: facet_wrap()的默认面板布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解 ggplot2 :: facet_wrap()的默认行为,即如何根据构面数量的增加来确定面板布局.

我已经阅读了?facet_wrap 帮助文件,并以有限的成功在Google上搜索了此主题.在一篇

图像中的图案使它看起来像 facet_wrap()试图制作一个正方形" ...

问题

  1. 那是正确的吗? facet_wrap 是否尝试渲染构面因此,从整体上来说,它们最像一个正方形行和列中的元素数量是多少?
  2. 如果不是,它实际上在做什么?图形参数会考虑在内吗?

绘制情节的代码

 #个加载库库(ggplot2)图书馆(ggpubr)#绘图功能facetPlots<-函数(构面,组= 8){# 样本数据df<-data.frame(Group = sample(LETTERS [1:groups],1000,replace = T),值=样本(1:10000,1000,替换= T),构面=因子(样本(1:构面,1000,替换= T)))#获取手段df<-汇总(list(Value = df $ Value),列表(组= df $ Group,构面= df $ Facet),均值)# 阴谋p1<-ggplot(df,aes(x = Group,y = Value,fill = Group))+geom_bar(stat ="identity",show.legend = FALSE)+facet_wrap(.〜Facet)+theme_bw()+主题(strip.text.x = element_text(大小= 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"))1}#将功能套用至清单plot_list<-lapply(c(1:25),facetPlots)#统一为一个情节情节<-ggpubr :: ggarrange(情节清单= plot_list) 

解决方案

以下是默认的行数和列数的计算方式:

  ncol<-上限(sqrt(n))nrow<-上限(n/ncol) 

显然, facet_wrap 倾向于更宽的网格,因为"大多数显示器都是大致矩形的"(根据文档).因此,列数将大于或等于行数.

以您的示例为例:

  n<-c(1:25)ncol<-上限(sqrt(n))nrow<-上限(n/ncol)data.frame(n,ncol,nrow) 

以下是计算的行数/列数:

 #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 

I'm trying to understand the default behavior of ggplot2::facet_wrap(), in terms of how the panel layout is decided as the number of facets increases.

I've read the ?facet_wrap help file, and also googled this topic with limited success. In one SO post, facet_wrap() was said to "return a symmetrical matrix of plots", but I did not find anything that explained what exactly the default behavior would be.

So next I made a series of plots which had increasing numbers of facets (code shown further down).

The pattern in the image makes it seem like facet_wrap() tries to "make a square"...

Questions

  1. Is that correct? Does facet_wrap try to render the facet panels so in totality they are most like a square, in terms of the number of elements in the rows and columns?
  2. If not, what is it actually doing? Do graphical parameters factor in?

Code that made the plot

# 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)  

解决方案

Here is how the default number of rows and columns are calculated:

ncol <- ceiling(sqrt(n))
nrow <- ceiling(n/ncol)

Apparently, facet_wrap tends to prefer wider grids, since "most displays are roughly rectangular" (according to the documentation). Hence, the number of columns would be greater than or equal to the number of rows.

For your example:

n <- c(1:25)

ncol <- ceiling(sqrt(n))
nrow <- ceiling(n/ncol)

data.frame(n, ncol, nrow)

Here are the computed numbers of rows/cols:

#   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

这篇关于ggplot2 :: facet_wrap()的默认面板布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆