将 ggplot facet_wrap 按组重新排序为列

Reordering ggplot facet_wrap by group as columns

我正在尝试使用 ggplot facet_wrap 按个人 (6x3) 图绘制集群。然而,次要情节的顺序不是我想要的。

我的数据和当前剧情:

df = data.frame(ID = rep(c(1:18),each=3), cluster = rep(c(1:6),each=9), 
            val = runif(54,5,8), date = rep(c(1:3),18))

ggplot(df, aes(date, val,)) + geom_bar(stat = 'identity') + 
            facet_wrap( ~ cluster*ID, nrow=3,ncol=6) + theme_bw()

但是,如您所见,这些图是按 ID 顺序排列的。我想要的是将每一列作为簇(即第 1 列包含 ID 为 1、2、3 的簇 1;第 2 列包含 ID 为 4、5、6 等的簇 2)。

有办法吗?请帮忙!谢谢!

我想你只需要用 dir 指示方向

ggplot(df, aes(date, val,)) + geom_bar(stat = 'identity') +
            facet_wrap(~cluster * ID, nrow = 3, ncol = 6, dir = "v") + theme_bw()