作者ardodo (米虫)
看板R_Language
标题[问题] ggplot2请教
时间Wed Jun 3 01:18:59 2015
各位板友好,我的问题是,如何将ggplot绘制的堆叠长条图,以堆叠的y值总合排序
附图说明:
http://imgur.com/bwv8fRn
我想要把这张图的x类别以y总合降幂排列,也就是变成a,c,b,d的顺序
请问该怎麽做呢?
以下为我画出此图的指令,求解
------------------------------------
##资料准备
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)
##运算资料
p=ggplot(data=hp)
p+geom_bar(binwidth=0.5,stat="identity")+
aes(x=class,y=amount,label=amount,fill=year)+
theme()
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 1.161.128.54
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/R_Language/M.1433265541.A.194.html
※ 编辑: ardodo (1.161.128.54), 06/03/2015 01:20:10
1F:→ celestialgod: levels(hp$class) = c("a", "c", "b", "d") 06/03 09:02
2F:→ celestialgod: 不想要手动改可以用 order 跟 tapply 06/03 09:05
3F:→ celestialgod: hp$class[order(tapply(hp$amount, hp$class, sum), 06/03 09:07
4F:→ celestialgod: decreasing = FALSE)] 06/03 09:07
5F:→ celestialgod: levels(hp$class) = hp$class[order(tapply( 06/03 09:07
6F:→ celestialgod: hp$amount, hp$class, sum), decreasing = FALSE)] 06/03 09:08
7F:→ ardodo: cel大您好,我尝试了您的code,出来的类别只有b,a,a,a耶? 06/04 09:02
8F:→ celestialgod: 详细的怎麽跑? 可以修改文章附上程式吗? 06/04 10:13