Re: [问题] ggplot2请教

楼主: ardodo (米蟲)   2015-06-04 10:15:15
更新一下自己的问题,我想要在ggplot上按照y值总合大小排序我的x类别,以下code
#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()
#↑这边新加入reorder试图将amount依照class重排
结果:http://imgur.com/DYsMqwl
#↑发现c,b,d有按照sum(amount)降幂排列,但是a没有
想请问各位版友有无解决办法呢?恳请赐教
作者: celestialgod (天)   2015-06-04 10:19:00
你没有照我跑啊...在hp$year那一行下面用这行: levels(hp$class) = hp$class[order(tapply(hp$amount, hp$class, sum), decreasing = FALSE)]

Links booklink

Contact Us: admin [ a t ] ucptt.com