在没有 y 定义变量的情况下订购 geom_bar()

Ordering geom_bar() without a y defined variable

y 只是 x 的计数时,有没有办法对 geom_bar() 中的条进行排序?

示例:

ggplot(dat) +
  geom_bar(aes(x = feature_1)) 

我尝试使用 reorder(),但它需要在 aes().

中定义一个 y 变量

编造数据:

dfexmpl <- data.frame(stringsAsFactors = FALSE,
         group = c("a","a","a","a","a","a",
                   "a","a","a","b","b","b","b","b","b","b","b","b",
                   "b","b","b","b","b","b"))

plot code - reorder 正在做按计数排列的工作:

dfexmpl %>%
group_by(group) %>%
mutate(count = n()) %>%
ggplot(aes(x = reorder(group, -count), y = count)) +
geom_bar(stat = "identity")

结果: