ggplot2: can't sort x axis by y value

后端 未结 1 1521
猫巷女王i
猫巷女王i 2020-12-17 04:16

I have problem in sorting x axis by y value in ggplot2: here is the code below

#Data
hp=read.csv(textConnection(
\"class,year,amount
a,99,100
a,100,200
a,101         


        
相关标签:
1条回答
  • 2020-12-17 04:26

    You need to give reorder the sum function, otherwise it defaults to using the mean function. Then, I put a - in front of amount to get the order reversed.

    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()
    

    enter image description here

    0 讨论(0)
提交回复
热议问题