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