我有这样的数据:
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)))
我需要像 barplot
和 beside = TRUE
和 beside = FALSE
(TRUE
for Cat
和 Comp
,以及 FALSE = Type
)。
有了这些数据,它将产生一个有 8 列的图(Comp
与 Cat
的交互(Comp = 2003 + Cat = A ; Comp = 2003 + Cat = B ; ... ; Comp = 2004 + Cat = D
)),每列有 2 个堆叠列(Type
的级别(Free
和 Used
)) 用于 Count
变量。
任何提示我怎么能做这种情节?我尝试在EXCEL中做一个例子,但我也失败了。
最佳答案
ggplot2
中类似:
ggplot(data, aes(x=interaction(Cat, Comp), y=Count, fill=Type)) +
geom_bar(position='stack', stat='identity')
要对附加变量(或两个)进行分组,您可以使用 facet_wrap
或 facet_grid
。
ggplot(data, aes(x=Cat, y=Count, fill=Type)) +
geom_bar(position='stack', stat='identity') +
facet_wrap( ~ Comp)
关于r - 具有 3 个分类变量的条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14940107/