作者ardodo (米蟲)
看板R_Language
標題Re: [問題] ggplot2請教
時間Thu Jun 4 10:15:15 2015
更新一下自己的問題,我想要在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沒有
想請問各位版友有無解決辦法呢?懇請賜教
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 163.14.191.172
※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1433384118.A.446.html
→ celestialgod: 你沒有照我跑啊... 06/04 10:19
推 celestialgod: 在hp$year那一行下面用這行: levels(hp$class) = hp 06/04 10:20
推 celestialgod: $class[order(tapply(hp$amount, hp$class, sum), d 06/04 10:20
推 celestialgod: ecreasing = FALSE)] 06/04 10:20