R中的堆积条形图给出错误

时间:2014-03-13 10:48:32

标签: r bar-chart

我正在尝试处理调查,我有Q1A到Q1H,答案是Q1,我想在R中的堆叠条形图中进行比较。所有这些都包含整数数据(分类)。

当我跑步时:

counts <- table(Q1A, Q1B, Q1C, Q1D, Q1E, Q1F, Q1G)
barplot(counts, main="Motivations for research",
xlab="motivation",
col=c('blue', 'green', 'red', 'orange', 'purple', 'yellow', 'grey'),
legend = rownames(counts))

我明白了:

 Error in.... 'height' must be a vector or a matrix

我做错了什么?这对ggplot2来说更容易吗?

/编辑:

Q1A和Q1B的输出输出为here

1 个答案:

答案 0 :(得分:2)

不确定你的条形图应该是什么样的,所以我有几个例子给你。我也在使用ggplot2plyr和&amp; combine个套餐

library(gdata)
library(plyr)
library(ggplot2)

q <- combine(q1a, q1b)
qSum <- count(q)

ggplot(qSum, aes(x = data, y = freq, fill = `source`)) +
  geom_bar(stat = 'identity')

ggplot(qSum, aes(x = data, y = freq)) +
  geom_bar(stat = 'identity') +
  facet_grid(`source` ~ .)