2012-07-18 34 views
1

在R的晶格包中,可以創建堆疊條形圖。我想有由類似的側幾個堆疊條形邊這一個:R中堆疊和並排條形圖的組合

barchart(mpg ~ as.factor(gear), data=mtcars, groups=cyl, stack=F, horizontal=F, auto.key=T) 

side-by-side barchart with covered bars

這幾乎是我所需要的。問題在於這些條是分層的,例如,對於中心的粉紅色條,有3個大致相同的值的分層條(17和22之間)。酒吧沒有堆積。後來畫的酒吧包括早些時候畫的酒吧。

對於堆疊酒吧以及並排酒吧和其他圖例,是否也可能具有不同的顏色/紋理?堆棧中的不同層次來自另一個因素。

回答

0
library(ggplot2) 
ggplot(mtcars, aes(x = factor(cyl), y = mpg, fill = factor(cyl))) + geom_bar(stat = "identity", colour = "black") + facet_wrap(~gear) 

enter image description here mtcars $ ID < - rownames(mtcars) ggplot(mtcars,AES(X =因子(齒輪),Y = MPG,填充=因子(CYL),基團= ID))+ geom_bar (STAT = 「同一性」,位置= 「躲閃」)

enter image description here

ggplot(mtcars, aes(x = factor(gear), y = mpg, colour = factor(cyl))) + geom_jitter() 

enter image description here

ggplot(mtcars, aes(x = factor(gear), y = mpg, colour = factor(cyl))) + geom_boxplot() 

enter image description here

+0

感謝您的回答。但酒吧仍然沒有堆放在一個組內。這就像有三個因素:1.因素(齒輪):堆積的鋼筋組2.因子(cyl):一組內的鋼筋;每個小節由多個級別組成3.另一個因素(例如mtcars數據幀中的carb)用於區分一堆小節中的級別 – porst17 2012-07-18 13:35:48

+0

我已經添加了一些新代碼。 – Thierry 2012-07-18 13:52:07