当使用stat="count"时,我想重新排列一个geom_bar图形,使其从高到低排序,以便我可以应用填充。
我尝试使用geom_bar(aes(x=reorder(x, -stat(count)), fill=type),但它没有起作用,并抛出错误“Error: stat_count requires the following missing aesthetics: x”。
library(ggplot2)
df <- data.frame(x = c("Bob", "James", "Mary", "Sally", "Timmy", "Sally", "Sally", "Bob", "Bob", "Mary"), type = c("A", "B", "A", "B", "B", "C", "B", "B", "A", "B"))
ggplot(df) +
geom_bar(aes(x = x, fill = type), stat = "count") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5))
我希望条形图从左到右按计数从高到低的顺序排列。