Nima
Nima

Reputation: 1540

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.

Upvotes: 0

Views: 126

Answers (1)

maloneypatr
maloneypatr

Reputation: 3622

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

Upvotes: 2

Related Questions