ggplot:根据条内的名称更改堆积条的顺序 [英] ggplot: change the order of stacked bars based on the a name within the bar
问题描述
ggplot(data,aes(x = ab,y = Freq / total,fill = Result))+
geom_bar(stat =identity)+
主题(strip.text.x = element_text(size = 8,angle = 0),
strip.background = element_rect(color =black,fill =#CCCCFF))+
ggtitle (H.somnus SIR%)+ ylab(%SIR)+
scale_y_continuous(labels = percent,breaks = seq(0,1,.1))+
theme_set(theme_barplot() )
以上是我正在使用的代码。数据是一个我已经融化的表格,但结果列的顺序是按字母顺序排列的,str(结果)是一个有4个级别的因子:像A,B,C,D。我想显示底部最大的条形图,并且订单将是D,B,C,A
谢谢
这是一个被黑客入侵的修复程序,但它有效。当使用stat =identity时,ggplot将按照遇到它们的顺序绘制堆积条形图。要按照D,B,C,A的顺序获得堆栈,请按如下方式对数据框架重新排序:
data < data [c(data $ Result ==D,
data $ Result ==B,
data $ Result ==C,
data $ Result ==A ),]
ggplot2 帮助文件在这方面可能会更好。
ggplot(data,aes(x=ab,y=Freq/total,fill=Result))+
geom_bar(stat="identity")+
theme(strip.text.x = element_text(size=8, angle=0),
strip.background = element_rect(colour="black", fill="#CCCCFF"))+
ggtitle("H.somnus SIR %")+ylab("% SIR")+
scale_y_continuous(labels=percent,breaks=seq(0,1,.1))+
theme_set(theme_barplot())
Above is the code that I am using. data is a table that I have melted, but the column 'result' is in an order that is alphabetical and the str(result) is a factor with 4 levels: like A,B,C,D. What I would like to display the bars with the largest bar on the bottom and the order would be D,B,C,A
Thanks
It's a bit of a hacked fix but it works. ggplot will plot the stacked bars in the order it encounters them when using stat = "identity". To get the stack in the order D,B,C,A reorder your data.frame like this:
data <- data[c(data$Result == "D",
data$Result == "B",
data$Result == "C",
data$Result == "A"),]
the entry in the ggplot2 help files could be better in this respect.
这篇关于ggplot:根据条内的名称更改堆积条的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!