2016-03-10 122 views
0

我製作了一個堆積條形圖,但是第三個酒吧的部分排列順序不同;這應該是底部的段現在是在頂部,看到圖像:在堆積條形圖(R ggplot2)中錯誤排序1個酒吧

stacked bar chart

當我改變x軸的順序,它精確地與所述第三條是相同的。我該如何解決這個問題?代碼有問題嗎?

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") 

補充:從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")​ 
+0

才能添加'dput(data.location)'的說,前三棒? 'dput(data.location [data.location $ Location%%%c('BRM','CG','DDO'),])' – rawr

+0

@rawr我添加了dput(data.location) –

+0

謝謝!儘管有了這些數據,但您的代碼仍然可以滿足您的需求。 @ wyldsoul的解決方案對你有用嗎?我也注意到你的圖片中沒有圖例,所以你用什麼其他代碼來製作圖表? – rawr

回答

0

使用下面的代碼,我得到的酒吧一致的順序。

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

enter image description here