I'm working on a problem where I want to create many plots based on some grouping vars. Some of these groups have many values I want to facet, while others only have a few. I want to display all of the facets to be the same size.
In this reprex, the 8 cyl group only has 2 facets while the others have 3. I want the 2 facets in the 8 cyl group to be the same size as facets in the other group, and the space for the 3rd missing facet to just be blank.

library(tidyverse)

for(i in unique(mtcars$cyl)){
  sub_data <- filter(mtcars, cyl == i)
  
  p <- ggplot(sub_data, aes(wt, drat)) +
    geom_point() +
    facet_wrap(~gear) +
    labs(title = paste(i, "cyl"))
  print(p)
}

推荐答案

我使用@stefan的建议来研究ggh4x,但最终放弃了design,只使用了facet_wrap2()和指定的行和列.我在上面使用的数据将有多达20个方面,因此使用facet_wrap2更有意义,并且避免了构建一个函数来可靠地声明design的需要.

如果没有环,它看起来是这样的.

library(tidyverse)
library(ggh4x)

p <- ggplot(mpg, aes(displ, hwy, colour = as.factor(cyl))) + geom_point() +
  labs(x = "Engine displacement", y = "Highway miles per gallon") +
  guides(colour = "none")

p + facet_wrap2(vars(class), nrow = 3, ncol = 4, trim_blank = FALSE)

我有点困惑,但随后接受了trim blank的论点.

如果你愿意,你可以查看我的here号项目的最终产品.

R相关问答推荐

如何将在HW上运行的R中的消息(错误、警告等)作为批处理任务输出

derrr summarise每个组返回多行?

然后根据不同的列值有条件地执行函数

S用事件解决物质平衡问题

为什么舍入POSIXct会更改能力以匹配等效的POSIXct?

如何将R中数据帧中的任何Nas替换为最后4个值

在不丢失空值的情况下取消列出嵌套列表

手动指定从相同数据创建的叠加图的 colored颜色

根据r中另一个文本列中给定的范围对各列求和

`-`是否也用于数据帧,有时使用引用调用?

判断函数未加载R中的库

R基于变量组合创建新的指标列

在REST API中使用参数R

网络抓取NBA.com

通过匹配另一个表(查找表)中的列值来填充数据表,并在另一个变量上进行内插

如何创建直方图与对齐的每月箱?

创建两个变量组合的索引矩阵

R中的交叉表

只有当我在循环的末尾放置一条print语句时,Foreach才会给出预期的输出

与另一个数据帧同名的变异/筛选列