首页 文章

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

提问于
浏览
0

我正在尝试处理一项调查,我有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 回答

  • 2

    不太确定你的条形图应该是什么样的,所以我有几个例子给你 . 我也在使用 ggplot2plyrcombine

    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` ~ .)
    

相关问题