r - ggplot2: can't sort x axis by y value -
i have problem in sorting x axis y value in ggplot2: here code below
#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() here result:

how sort x axis c b d, amount sorted decreasing 450, 290, 150, 90. should do?
you need give reorder sum function, otherwise defaults using mean function. then, put - in front of amount 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()
Comments
Post a Comment