[英]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。我的问题是双重的:
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.