我在ggplot2中按x值排序x轴有问题:这是下面的代码
#Data
hp=read.csv(textConnection(
"class,year,amount
a,99,100
a,100,200
a,101,150
b,100,50
b,101,100
c,102,70
c,102,80
c,103,90
c,104,50
d,102,90"))
hp$year=as.factor(hp$year)
#Plotting
p=ggplot(data=hp)
p+geom_bar(binwidth=0.5,stat="identity")+ #
aes(x=reorder(class,amount),y=amount,label=amount,fill=year)+
theme()
Run Code Online (Sandbox Code Playgroud)
结果如下:

如何按acbd对x轴进行排序,其数量按450,290,150,90减少排序.我该怎么办?
Jot*_*ota 17
您需要重新排序sum函数,否则默认使用该mean函数.然后,我-在前面放了一个amount令逆转的订单.
p=ggplot(data=hp)
p+geom_bar(binwidth=0.5,stat="identity")+ #
aes(x=reorder(class,-amount,sum),y=amount,label=amount,fill=year)+
theme()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8730 次 |
| 最近记录: |