2013-02-18 67 views
3

我有这样一个数据:Barplot 3分类变量

data <- data.frame(Comp = factor(rep(2003:2004, each = 8)), 
        Cat = rep(letters[1:4], 4), 
        Type = rep(c('Free','Used'), each = 4), 
        Count = trunc(rnorm(16,30,2))) 

我需要的东西就像一个barplotbeside = TRUEbeside = FALSETRUECatCompFALSE = Type)。

有了这些数据,这将导致与8列的曲线图(的Comp与相互作用CatComp = 2003 + Cat = A ; Comp = 2003 + Cat = B ; ... ; Comp = 2004 + Cat = D)),每一个有2分堆叠列Count变量(的TypeFreeUsed和)的水平)。

任何提示如何做这种情节?我试图在EXCEL中做一个例子,但是我也没有做到这一点。

回答

4

同样在ggplot2

ggplot(data, aes(x=interaction(Cat, Comp), y=Count, fill=Type)) + 
    geom_bar(position='stack', stat='identity') 

要在一个额外的变量(或两个)基团可以使用facet_wrapfacet_grid

ggplot(data, aes(x=Cat, y=Count, fill=Type)) + 
    geom_bar(position='stack', stat='identity') + 
    facet_wrap(~ Comp) 
+0

同样的问题:你知道是否有任何选项可以将每个Comp的'Cat'分组?像这样:'barplot(VADeaths,在旁边= TRUE)' – Rcoster 2013-02-18 16:06:19

+0

看到我的编辑使用'facet_wrap'。 – Justin 2013-02-18 16:11:35

+0

Exactaly!最后一个问题:有一种简单的方法可以在每个栏的中间写入数值? – Rcoster 2013-02-18 16:22:58

4

lattice

barchart(Count~interaction(Cat,Comp),groups=Type,data=data,auto.key=T,stack=T) 

enter image description here

另一种方式来组,从评论:

barchart(Count~Cat|factor(Comp),groups=Type,data=data,auto.key=T,stack=T) 

enter image description here

+0

几乎在那里!你知道是否有任何选项可以将每个“Comp”的“Cat”组合起来?像这样:'barplot(VADeaths,旁边= TRUE)' – Rcoster 2013-02-18 16:04:48

+0

+1!很好的使用'互动'! – agstudy 2013-02-18 16:07:47

+0

谢谢兄弟!最后一个问题:有一种简单的方法可以在每个栏的中间写入数值? – Rcoster 2013-02-18 16:25:49