【发布时间】:2020-05-21 07:22:01
【问题描述】:
这是我的 CSV 文件:
Number,inst,pour,Type
One Smell,450,66.96%,OO smells
One Smell,237,36.18%,Both
One Smell,255,49.71%,Android-specific smells
Two Smells,160,16.22%,OO smells
Two Smells,143,21.83%,Both
Two Smells,109,31.19%,Android-specific smells
Three Smells,109,16.64%,Both
Three Smells,61,7.29%,OO smells
Three Smells,49,11.89%,Android-specific smells
Four Smells,66,10.08%,Both
Four Smells,33,6.24%,Android-specific smells
Four Smells,32,4.91%,OO smells
Five Smells,35,5.34%,Both
Five Smells,24,3.57%,OO smells
Five Smells,4,0.78%,Android-specific smells
Six Smells,22,3.36%,Both
Six Smells,7,1.04%,OO smells
Six Smells,1,0.19%,Android-specific smells
Seven smells,17,2.60%,Both
Eight smells,14,2.14%,Both
Nine Smells,9,1.37%,Both
Ten smells,2,0.31%,Both
Eleven smells,1,0.15%,Both
我根据类型(Android 特定气味、OO 气味、两者)进行分组,结果如下:
这是代码:
library(ggplot2)
theme_set(theme_classic())
# Plot
g <- ggplot(Co.oooa, aes(x=reorder(Number, inst), y=inst, fill=Type))+
theme(axis.text.x = element_text(angle=0, vjust=0.67, size = 10), axis.text.y = element_text(angle=0, vjust=0.6, size=10))
g + geom_bar(stat="identity", width = 0.57, position ="dodge" )+
geom_text(aes(label=pour, group=Type), position=position_dodge(width=0.67), hjust=0.0001, size=2.5 )+
scale_fill_manual(values=c( "gray", "royalblue1", "red"))+
coord_flip()
我需要根据它们的长度重新排序条。例如,对于一种气味,第一个栏应该是OO smells,然后是Android-specific smells,最后是Both。
【问题讨论】:
-
能否让您的问题可重现?请不要使用图像数据,因为没有大量不必要的努力就无法使用它们。 minimal reproducible example 提供有关为问题提供数据的指导。
-
@DavideBrex 谢谢,但是对于简单的条形图有帮助,但对于分组条形图它不起作用
-
我遇到了同样的问题,我found this post 比其他人更有帮助。