【发布时间】:2018-09-04 18:16:11
【问题描述】:
我在mydata中有以下内容:
Class Category
"One" "A"
"One" "A"
"Two" "A"
"Two" "A"
"Three" "B"
"Three" "B"
"One" "C"
"Two" "C"
我用ggplot2:
ggplot(mydata) +
aes(x = Category, fill = Class) +
geom_bar()
我得到这个结果:
我注意到“类”项按字母顺序显示。但我希望可以按如下方式订购它们:
- 特设,因此请选择确切的顺序
- 按照出现在数据中的顺序,所以在这种情况下,
One, Two, Three - 在数据中出现的相反顺序:
Three, Two, One
感谢您的回答。
澄清
如有疑问,以下是上述数据的完整工作示例:
Class <- c("One", "One", "Two", "Two", "Three", "Three", "One", "Two", "Four")
Category <- c("A", "A", "A", "A", "B", "B", "C", "C", "C")
mydata <- data.frame(Class, Category)
ggplot(mydata) +
aes(x = Category, fill = Class) +
geom_bar()
右边生成的Class键的顺序是:
Four, One, Three, Two
我想控制生成的密钥中项目的顺序。 (颜色不太重要。)
【问题讨论】: