我正在尝试处理调查,我有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。
答案 0 :(得分:2)
不确定你的条形图应该是什么样的,所以我有几个例子给你。我也在使用ggplot2
,plyr
和&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` ~ .)