使用 facet_grid 对 ggplot 中的方面进行排序

Sorting facets in ggplot using facet_grid

我已经搜索过 SO 试图找到解决这个问题的方法。我找到了一些答案,但是当我应用它们时,它们并没有像我预期的那样工作(或者也许我只是做错了什么)。基本上,我有一个数据集,我正在尝试使用 facet_grid。我的问题是双重的:

  1. 我希望根据数值对网格进行排序
  2. 我有一个分组变量,我也想按数值(降序)排序,但我希望能够将升序计数分配给一个组(即,最高的组获得 1,第二高的获得 a 2 等)这样我就可以 select 首先是最高组,然后是第二高组,等等。我有很多组,这些组的值和名称是随机的,所以我不想手动分配值。

这是我正在尝试做的一个例子:

df <- data.frame(grp.name = c("T","F","P","T","F","P","T","F","P","T","F","P"),
                 grp.num   = c(0.9954,0.8754,0.5006,0.9954,0.8754,0.5006,0.9954,0.8754,0.5006,0.9954,0.8754,0.5006),   
                 ind.name  = c("L","N","M","C","A","B","I","H","G","D","F","E"),   
                 amount = c(48.41234, 48.12343, 46.83546, 25.9454, 26.01568, 24.946454, 21.1, 21.4545, 20.1, 20.8, 21.5644, 16.5) )


df <- df[order(-df$grp.num,-df$amount),]

t <-  ggplot(data=subset(df,grp.name=="T"), aes(grp.name, amount, fill=grp.name, group=grp.name, shape=grp.name, facets=grp.name)) +
  geom_col(width=0.5, position = position_dodge(width=0.6)) + 
  facet_grid(grp.name + paste0("Number: ",grp.num)  ~ ind.name + paste0("Number: ",amount),switch = "y") 
t

在这种情况下,我只对组 T 进行子集化的原因是因为我想绘制出最高的 10 个组,然后使用 grid.arrange 将它们堆叠在另一个图中。我希望能够在不知道名称的情况下对第 1 组、第 2 组、第 3 组等进行子集化,因为这些名称再次发生变化。

这是我得到的。不是按 amount 对构面进行排序,而是按 ind.name 的字母顺序对它们进行排序。我首先想要“L”组,因为它的数量最多,其次是“C”,然后是“I”,然后是“D”。同样,我不想手动执行此操作,因为这些值会发生变化,并且我正在对许多组重做这些图。

这个怎么样:

t <-  ggplot(data=subset(df,grp.name=="T"), aes(grp.name, amount, fill=grp.name, group=grp.name, shape=grp.name)) +
  geom_col(width=0.5, position = position_dodge(width=0.6)) + 
  facet_grid(grp.name + paste0("Number: ",grp.num)  ~ reorder(ind.name, -amount, mean) + paste0("Number: ",amount),switch = "y") 
t

有时,在传递给 ggplot 函数之前进行数据准备更简单。

分面顺序适用于因子,因此将 ind.name 转换为按 amount 排序的因子。根据因子顺序创建 grp_nr

根据 OP 的评论和新数据集进行了修订。

library(ggplot2)
library(forcats)
library(dplyr)

df %>%
  mutate(ind.name = fct_rev(fct_reorder(ind.name, amount)),
         amount = round(amount, 3),
         grp.num_lev = as.integer(fct_rev(factor(grp.num))),
         grp.num = round(grp.num, 3))%>%
  filter(grp.num_lev==1) %>%
  ggplot(aes(grp.name, amount, fill=grp.name, group=grp.name, shape=grp.name)) +   
  geom_col(width=0.5, position = position_dodge(width=0.6)) +   
  facet_grid(grp.name + paste0("Number: ", grp.num)  ~ ind.name + paste0("Number: ", amount), switch = "y")

reprex package (v2.0.1)

于 2021-12-03 创建

数据

df <- data.frame(grp.name   = c("T","F","P","T","F","P","T","F","P","T","F","P"),
                 grp.num   = c(0.9954,0.8754,0.5006,0.9954,0.8754,0.5006,0.9954,0.8754,0.5006,0.9954,0.8754,0.5006),   
                 ind.name  = c("L","N","M","C","A","B","I","H","G","D","F","E"),   
                 amount = c(48.41234, 48.12343, 46.83546, 25.9454, 26.01568, 24.946454, 21.1, 21.4545, 20.1, 20.8, 21.5644, 16.5) )