Alberto
Alberto

Reputation: 2982

barplot not working

bars <- list(v=1:10, a=2:11)
barplot(bars, col=c("green", "black"))

I can't understand why this code doesn't work, I get this error:

Error in -0.01 * height : non-numeric argument to binary operator

UPDATE: I need a grouped barplot, with 10 groups and two bars in each group

Upvotes: 1

Views: 6236

Answers (1)

Alpha
Alpha

Reputation: 807

Probably you want this:

bars <- cbind(1:10, 2:11)
barplot(bars, beside = TRUE, col = c("green", "black"))

The error appeared because bars is a list and height has to be either a vector or matrix of values describing the bars.

Edit:

In order to get 10 groups of 2 bars you need to transpose the bars matrix

barplot(t(bars), beside = TRUE, col = c("green", "black"))

enter image description here

Upvotes: 11

Related Questions