简体   繁体   English

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

[英]Sorting facets in ggplot using facet_grid

I have scoured SO trying to find a fix for this.我已经搜遍了试图找到解决这个问题的方法。 I've found a few answers, but when I apply them, they don't work as I am intending (or maybe I"m just doing something wrong). Basically, I have a dataset that I am trying to plot using facet_grid. My problem is twofold:我找到了一些答案,但是当我应用它们时,它们并没有按我的意图工作(或者我可能只是做错了什么)。基本上,我有一个数据集,我正在尝试使用 facet_grid 进行 plot。我的问题是双重的:

  1. I want the the grids to be sorted according to a numeric value我希望根据数值对网格进行排序
  2. I have a grouping variable that I also want sorted by a numeric value (descending) but I want to be able to assign an ascending count number to a group (ie, highest group gets a 1, 2nd highest gets a 2, etc) so that I can select the highest group first, then the 2nd highest group, etc. I have many many groups and the values and names that come with those groups are random, so I don't want to manually assign the values.我有一个分组变量,我也希望按数值(降序)排序,但我希望能够将升序计数分配给一个组(即,最高组获得 1,第二高获得 2 等)所以我可以先 select 最高组,然后是第二高组,等等。我有很多组,这些组附带的值和名称是随机的,所以我不想手动分配这些值。

Here is an example of what I am trying to do:这是我正在尝试做的一个例子:

  grp.name   = c("T","F","P","T","F","P","T","F","P","T","F","P"), 
  grp.num   = c(0.995,0.875,0.500,0.995,0.875,0.500,0.995,0.875,0.500,0.995,0.875,0.500),
  ind.name  = c("L","N","M","C","A","B","I","H","G","D","F","E"),
  amount = c(48.4, 48.1, 46.8, 25.9, 26, 24.9, 21.1, 21.4, 20.1, 20.8, 21.5, 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

My reason for subsetting just group T in this case is because I want to plot out the 10 highest groups and then stack them in another plot using grid.arrange.在这种情况下,我只对组 T 进行子集化的原因是因为我想 plot 出 10 个最高组,然后使用 grid.arrange 将它们堆叠在另一个 plot 中。 I would like to be able to just subset group 1, then 2, then 3, etc without knowing the name, because, again, these names change.我希望能够在不知道名称的情况下仅对组 1、2、3 等进行子集化,因为这些名称再次发生了变化。

Here is what I get.这是我得到的。 Instead of sorting the facets by amount it is sorting them alphabetically by ind.name .它不是按amount对构面进行排序,而是按ind.name的字母顺序对它们进行排序。 I would like the "L" group first, as it has the highest amount, followed by "C", then "I", then "D".我想要“L”组,因为它的数量最多,然后是“C”,然后是“I”,然后是“D”。 Again, I don't want to do this manually, as these values change and I am redoing these plots over many many groups.同样,我不想手动执行此操作,因为这些值会发生变化,并且我要在许多组上重做这些图。
在此处输入图像描述

How about this:这个怎么样:

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

在此处输入图像描述

Sometimes, its simpler to carry out the preparation of data prior to passing to the ggplot function.有时,在传递给 ggplot function 之前进行数据准备会更简单。

Faceting order works on factors, so convert ind.name to a factor ordered by amount .分面顺序适用于因子,因此将ind.name转换为按amount排序的因子。 Create a grp_nr based on factor order.根据因子顺序创建一个grp_nr

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

    df %>%
      mutate(ind.name = fct_rev(fct_reorder(ind.name, amount)),
             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")

Created on 2021-12-03 by the reprex package (v2.0.1)reprex package (v2.0.1) 于 2021 年 12 月 3 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM