stacked bar plot in R gives error

I am trying to process a survey, I have Q1A to Q1H with answers of Q1 which I want to compare in a stacked barplot in R. All of them contain integer data (categorical).

When I run:

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))

I get:

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

What am I doing wrong? is this easier to do with ggplot2?

/edit:

output of dput for Q1A and Q1B is here.

Answers


Not really sure what your bar plot is supposed to look like, so I have a couple examples for you. I'm also using the ggplot2, plyr, & combine packages

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

Need Your Help

Is there an equivalent Matlab dot function in numpy?

python matlab numpy

Is there an equivalent Matlab dot function in numpy?

Filling a view model from entity data, getting convertion error

c# asp.net-mvc linq entity-framework

I am trying to fill a ViewModel from a LINQ to Entities query but I am getting the error