id
我正在尝试按每个方面从高到低对条形进行排序facet_var
。下面的脚本对大多数方面都运行良好,但不包括NGRRR.V
。不知道哪里出了问题,我该如何修复?
结果
我尝试过什么?
library(data.table)
library(tidyverse)
dat <- as.data.table(df)
dat[, ord := sprintf("%02i", frank(dat, facet_var, -value, ties.method = "first"))]
ggplot(dat, aes(x = ord, y = value, fill = id)) +
geom_col() +
facet_wrap(~ facet_var, scales = "free", drop = TRUE) +
scale_x_discrete(labels = dat[, setNames(as.character(id), ord)]) +
labs(x = "") +
theme_bw() +
theme(legend.title = element_blank(),
legend.key = element_blank(),
legend.background = element_blank(),
legend.position = "none",
strip.text = element_text(size = 8),
axis.text.x = element_text(angle = 45, hjust = 1, size = 8))
数据
> dput(df)
structure(list(id = structure(c(12L, 1L, 4L, 6L, 8L, 5L, 11L,
7L, 3L, 2L, 9L, 10L, 12L, 1L, 4L, 6L, 8L, 5L, 11L, 7L, 3L, 2L,
9L, 10L, 12L, 1L, 4L, 6L, 8L, 5L, 11L, 7L, 3L, 2L, 9L, 10L, 12L,
1L, 4L, 6L, 8L, 5L, 11L, 7L, 3L, 2L, 9L, 10L, 12L, 1L, 4L, 6L,
8L, 5L, 11L, 7L, 3L, 2L, 9L, 10L, 12L, 1L, 4L, 6L, 8L, 5L, 11L,
7L, 3L, 2L, 9L, 10L, 12L, 1L, 4L, 6L, 8L, 5L, 11L, 7L, 3L, 2L,
9L, 10L, 12L, 1L, 4L, 6L, 8L, 5L, 11L, 7L, 3L, 2L, 9L, 10L, 12L,
1L, 4L, 6L, 8L, 5L, 11L, 7L, 3L, 2L, 9L, 10L, 12L, 1L, 4L, 6L,
8L, 5L, 11L, 7L, 3L, 2L, 9L, 10L, 12L, 1L, 4L, 6L, 8L, 5L, 11L,
7L, 3L, 2L, 9L, 10L, 12L, 1L, 4L, 6L, 8L, 5L, 11L, 7L, 3L, 2L,
9L, 10L, 12L, 1L, 4L, 6L, 8L, 5L, 11L, 7L, 3L, 2L, 9L, 10L), levels = c("973",
"1264", "1472", "1993", "2097", "2221", "2987", "3655", "4009",
"5890", "16000", "16471"), class = "factor"), Q = c(6.59, 6.67,
1.74, 9.92, 15.55, 3.76, 5.72, 3.83, 4.95, 3.58, 6.3, 2.96, 6.59,
6.67, 1.74, 9.92, 15.55, 3.76, 5.72, 3.83, 4.95, 3.58, 6.3, 2.96,
6.59, 6.67, 1.74, 9.92, 15.55, 3.76, 5.72, 3.83, 4.95, 3.58,
6.3, 2.96, 6.59, 6.67, 1.74, 9.92, 15.55, 3.76, 5.72, 3.83, 4.95,
3.58, 6.3, 2.96, 6.59, 6.67, 1.74, 9.92, 15.55, 3.76, 5.72, 3.83,
4.95, 3.58, 6.3, 2.96, 6.59, 6.67, 1.74, 9.92, 15.55, 3.76, 5.72,
3.83, 4.95, 3.58, 6.3, 2.96, 6.59, 6.67, 1.74, 9.92, 15.55, 3.76,
5.72, 3.83, 4.95, 3.58, 6.3, 2.96, 6.59, 6.67, 1.74, 9.92, 15.55,
3.76, 5.72, 3.83, 4.95, 3.58, 6.3, 2.96, 6.59, 6.67, 1.74, 9.92,
15.55, 3.76, 5.72, 3.83, 4.95, 3.58, 6.3, 2.96, 6.59, 6.67, 1.74,
9.92, 15.55, 3.76, 5.72, 3.83, 4.95, 3.58, 6.3, 2.96, 6.59, 6.67,
1.74, 9.92, 15.55, 3.76, 5.72, 3.83, 4.95, 3.58, 6.3, 2.96, 6.59,
6.67, 1.74, 9.92, 15.55, 3.76, 5.72, 3.83, 4.95, 3.58, 6.3, 2.96,
6.59, 6.67, 1.74, 9.92, 15.55, 3.76, 5.72, 3.83, 4.95, 3.58,
6.3, 2.96), value = c(4.58, 4.78, 1.18, 8.59, 11.89, 3.03, 4.19,
2.8, 3.65, 2.54, 4.57, 2.19, 0.1, 0.1, 0.02, 0.08, 0.18, 0.03,
0.08, 0.06, 0.07, 0.05, 0.09, 0.04, 0.78, 0.81, 0.17, 0.64, 1.5,
0.29, 0.69, 0.47, 0.61, 0.43, 0.77, 0.36, 0.37, 0.38, 0.08, 0.3,
0.7, 0.13, 0.33, 0.22, 0.29, 0.21, 0.37, 0.17, 0.07, 0.07, 0.01,
0.05, 0.12, 0.02, 0.06, 0.04, 0.05, 0.04, 0.06, 0.03, 0.23, 0.24,
0.05, 0.2, 0.45, 0.09, 0.21, 0.14, 0.18, 0.13, 0.23, 0.11, 0.34,
0.35, 0.06, 0.25, 0.6, 0.11, 0.29, 0.2, 0.26, 0.19, 0.33, 0.15,
0.34, 0.34, 0.06, 0.25, 0.6, 0.11, 0.29, 0.2, 0.26, 0.19, 0.33,
0.15, 0.22, 0.22, 0.04, 0.16, 0.38, 0.07, 0.19, 0.13, 0.17, 0.12,
0.21, 0.1, 0.15, 0.15, 0.03, 0.11, 0.26, 0.05, 0.12, 0.08, 0.11,
0.08, 0.14, 0.06, 0.34, 0.34, 0.07, 0.25, 0.6, 0.12, 0.28, 0.19,
0.24, 0.18, 0.32, 0.14, 2.7, 2.88, 0.76, 7.18, 8.41, 2.38, 2.63,
1.71, 2.26, 1.53, 2.77, 1.38, 0, 0, 0, 0, 0.01, 0, 0, 0, 0, 0,
0, 0), facet_var = c("ALLZN", "ALLZN", "ALLZN", "ALLZN", "ALLZN",
"ALLZN", "ALLZN", "ALLZN", "ALLZN", "ALLZN", "ALLZN", "ALLZN",
"IRNGT", "IRNGT", "IRNGT", "IRNGT", "IRNGT", "IRNGT", "IRNGT",
"IRNGT", "IRNGT", "IRNGT", "IRNGT", "IRNGT", "KARAM", "KARAM",
"KARAM", "KARAM", "KARAM", "KARAM", "KARAM", "KARAM", "KARAM",
"KARAM", "KARAM", "KARAM", "KRM.G", "KRM.G", "KRM.G", "KRM.G",
"KRM.G", "KRM.G", "KRM.G", "KRM.G", "KRM.G", "KRM.G", "KRM.G",
"KRM.G", "KRWRW", "KRWRW", "KRWRW", "KRWRW", "KRWRW", "KRWRW",
"KRWRW", "KRWRW", "KRWRW", "KRWRW", "KRWRW", "KRWRW", "MNGTR",
"MNGTR", "MNGTR", "MNGTR", "MNGTR", "MNGTR", "MNGTR", "MNGTR",
"MNGTR", "MNGTR", "MNGTR", "MNGTR", "NGRRR", "NGRRR", "NGRRR",
"NGRRR", "NGRRR", "NGRRR", "NGRRR", "NGRRR", "NGRRR", "NGRRR",
"NGRRR", "NGRRR", "NGRRR.M", "NGRRR.M", "NGRRR.M", "NGRRR.M",
"NGRRR.M", "NGRRR.M", "NGRRR.M", "NGRRR.M", "NGRRR.M", "NGRRR.M",
"NGRRR.M", "NGRRR.M", "NGRRR.V", "NGRRR.V", "NGRRR.V", "NGRRR.V",
"NGRRR.V", "NGRRR.V", "NGRRR.V", "NGRRR.V", "NGRRR.V", "NGRRR.V",
"NGRRR.V", "NGRRR.V", "RAUPR", "RAUPR", "RAUPR", "RAUPR", "RAUPR",
"RAUPR", "RAUPR", "RAUPR", "RAUPR", "RAUPR", "RAUPR", "RAUPR",
"TTK.W", "TTK.W", "TTK.W", "TTK.W", "TTK.W", "TTK.W", "TTK.W",
"TTK.W", "TTK.W", "TTK.W", "TTK.W", "TTK.W", "TUKTK", "TUKTK",
"TUKTK", "TUKTK", "TUKTK", "TUKTK", "TUKTK", "TUKTK", "TUKTK",
"TUKTK", "TUKTK", "TUKTK", "TUTKR", "TUTKR", "TUTKR", "TUTKR",
"TUTKR", "TUTKR", "TUTKR", "TUTKR", "TUTKR", "TUTKR", "TUTKR",
"TUTKR")), class = "data.frame", row.names = c(NA, -156L))
问题在于,你只用一个零填充了“排名”,而不是两个零。因此,你可以通过用两个零填充来解决问题,即
或者不要用零填充,而是将等级转换为 a
factor
(这样将id
s 添加为标签的方法仍然有效):