我试图了解ggplot2::facet_wrap()
的默认行为,即如何根据构面数量的增加确定面板布局。
我已经阅读了?facet_wrap
帮助文件,并以有限的成功搜索了该主题。在一个SO post中,据说facet_wrap()
“返回了一个对称的图矩阵”,但是我没有找到任何能解释默认行为确切的东西。
因此,接下来我进行了一系列绘图,这些绘图的构面数量不断增加(代码显示在下方)。
图像中的图案使facet_wrap()
看起来像是要“做成正方形” ...
问题
facet_wrap
是否尝试渲染构面
因此,就整体而言,它们最像正方形
行和列中的元素数量是多少?绘制情节的代码
# 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)
答案 0 :(得分:2)
以下是默认的行数和列数的计算方式:
Main->pyx
显然,ncol <- ceiling(sqrt(n))
nrow <- ceiling(n/ncol)
倾向于更宽的网格,因为“ 大多数显示器都是大致矩形的”(根据文档)。因此,列数将大于或等于行数。
以您的示例为例:
facet_wrap
以下是计算的行数/列数:
n <- c(1:25)
ncol <- ceiling(sqrt(n))
nrow <- ceiling(n/ncol)
data.frame(n, ncol, nrow)