简体   繁体   English

堆积条形图(R ggplot2)中的顺序错误的条形为1条

[英]1 bar in wrong order in stacked bar chart (R ggplot2)

I made a stacked bar chart, however the segments of the 3rd bar are in a different order; 我制作了一个堆积的条形图,但是第三个条形图的各个部分的顺序不同。 the segment that should be on the bottom is now on the top, see image: 现在应该位于底部的细分现在位于顶部,请参见图片:

堆积条形图

When I change the order of the x axis, it does exactly the same with the 3rd bar. 当我更改x轴的顺序时,它与第3条完全相同。 How can I solve this? 我该如何解决? Something wrong with the code? 代码有问题吗?

library('ggplot2')
bar <- ggplot(data.location, aes(Location, value, fill=variable))
bar + stat_summary(fun.y=mean, geom="bar", position="stack")+labs(x="Location", y="value", fill="variable")

Added: output from dput(data.location) 补充:dput(data.location)的输出

dput(data.location[data.location$Location %in% c('BRM', 'CG', 'DDO'),])

structure(list(Location = structure(c(2L, 3L, 5L, 2L, 3L, 5L, 
2L, 3L, 5L, 2L, 3L, 5L), .Label = c("BA", "BRM", "CG", "CH", 
"DDO", "DR", "FB", "GG", "GI", "GQS", "HC", "HS", "LL1", "LL2", 
"MOW", "PP", "TP", "TR", "TRD", "WB"), class = "factor"), Zone = c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), variable = structure(c(1L, 
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L), .Label = c("A", "B", "C", "D"
), class = "factor"), value = c(425.810194245673, 815.265455416096, 
735.274721619422, 997.041922511793, 2147.03610300279, 1210.08829970945, 
0, 177.692085889937, 173.266014794846, 406.498315338813, 1293.35105648741, 
234.022025228589)), .Names = c("Location", "Zone", "variable", 
"value"), row.names = c(2L, 3L, 5L, 22L, 23L, 25L, 42L, 43L, 
45L, 62L, 63L, 65L), class = "data.frame")​

Using the following code, I get consistent order of the bars. 使用下面的代码,我得到条的一致顺序。

ggplot(data.location,aes(Location,value,fill=variable,order=variable))+
geom_bar(stat="identity")

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM