如何通过第三个变量对条形图中的条形图进行分组?
我想在基数R中实现这一点,例如,没有像this related question那样的ggplot2。在another related question中,组的组被标记,但没有(视觉上)分组 - 如上例所示 - 使得图形难以阅读。
示例数据:
groups = c("A", "B")
choices = c("orange", "apple", "beer")
supergroups = c("fruits", "non-fruits")
dat <- data.frame(
group = rep(groups, c(93, 94)),
choice = factor(c(
rep(choices, c(51, 30, 12)),
rep(choices, c(47, 29, 18))
),
levels = choices
),
supergroup = c(
rep(supergroups, c(81, 12)),
rep(supergroups, c(76, 18))
)
)
barplot(table(dat), beside = TRUE)
返回错误:
Error in barplot.default(table(dat), beside = TRUE) :
'height' must be a vector or a matrix