1

I'm trying to make a barplot with grouped bar, but I have this error:

'height' must be a vector or a matrix

And I don't know why. My code is ...

rebDef=sample(50:100,14,replace=F)
rebOf=sample(20:40, 14, replace=F)
rebTot=sample(70:140, 14, replace=F)
data=data.frame(rebDef,rebOf,rebTot)
v <- c("Equipo A", "Equipo B", "Equipo C", "Equipo D", "Equipo E", "Equipo F", "Equipo G", "Equipo H", "Equipo I", "Equipo J", "Equipo K", "Equipo M", "Equipo N", "Equipo O")
names <- data.frame(v)
rownames(data) <- names[, 1]

barplot(data, beside=T , legend.text=T, col=c("red" , "green", "blue"), ylim=c(0,140), ylab="height")

And my dput from data is ...

structure(list(rebDef = c(93L, 59L, 80L, 58L, 71L, 70L, 83L, 
77L, 99L, 52L, 84L, 98L, 100L, 86L), rebOf = c(20L, 38L, 32L, 
35L, 36L, 29L, 30L, 26L, 39L, 22L, 25L, 28L, 23L, 33L), rebTot = c(99L, 
105L, 107L, 72L, 118L, 87L, 115L, 88L, 85L, 131L, 137L, 84L, 
126L, 136L)), .Names = c("rebDef", "rebOf", "rebTot"), row.names = c("Equipo A", 
"Equipo B", "Equipo C", "Equipo D", "Equipo E", "Equipo F", "Equipo G", 
"Equipo H", "Equipo I", "Equipo J", "Equipo K", "Equipo M", "Equipo N", 
"Equipo O"), class = "data.frame")

Finally, the content of the data frame called data is ...

         rebDef rebOf rebTot
Equipo A     93    20     99
Equipo B     59    38    105
Equipo C     80    32    107
Equipo D     58    35     72
Equipo E     71    36    118
Equipo F     70    29     87
Equipo G     83    30    115
Equipo H     77    26     88
Equipo I     99    39     85
Equipo J     52    22    131
Equipo K     84    25    137
Equipo M     98    28     84
Equipo N    100    23    126
Equipo O     86    33    136

What am I doing wrong?

José Carlos
  • 2,850
  • 12
  • 59
  • 95

1 Answers1

5

I think the issue might be that you are trying to pass a DataFrame as the height parameter. This should only take a vector or a matrix. You can try something like this:

barplot(as.matrix(data), beside=T , legend.text=T, col=c("red" , "green", 
"blue"), ylim=c(0,140), ylab="height")

Hopefully this helps you out.

Master_Mahan
  • 124
  • 4
  • It works!!! But the bars appear to me grouped by col and not by row. Do you know how I can say to barplto which group by row? – José Carlos Dec 01 '17 at 17:06
  • 1
    Yeah, all you need to do is transpose the matrix by adding a t() around the as.matrix call: barplot(t(as.matrix(data)), beside=T , legend.text=T, col=c("red" , "green", "blue"), ylim=c(0,140), ylab="height") Give that one a try and see if helps. – Master_Mahan Dec 01 '17 at 17:08
  • It works!!! Thank you!!! Sorry, but now I've got two new problems :( The graph is correct but doesn't appear to me all the labels of each group of bars and the legend text is over the bars and I would like to be on the right of the graph ... Is possible to do all of these? – José Carlos Dec 01 '17 at 17:12
  • Sorry, If I make the pic wider I can see all the names of group bars. But the legend is still over the graphs. – José Carlos Dec 01 '17 at 17:18
  • 1
    y <- t(as.matrix(data)) par(mfrow=c(1, 1), mar=c(5, 5, 4, 8)) barplot(t(as.matrix(data)), beside=TRUE , legend.text=TRUE, col=c("red" , "green", "blue"), ylim=c(0,140), ylab="height", args.legend = list(x = "topright",y = max(colSums(y)),bty = "n",inset=c(-0.15, 0)), las = 2 ) – Master_Mahan Dec 01 '17 at 17:27
  • 1
    You can try the above code. I named the transposed matrix as "y" since it was easier to read in the code. I changed the team names to display as vertical. The following two links were very useful: https://stackoverflow.com/questions/9981929/how-to-display-all-x-labels-in-r-barplot https://stackoverflow.com/questions/18688847/position-legend-of-a-stacked-bar-plot – Master_Mahan Dec 01 '17 at 17:28
  • Thank you!!! Everything is alright now, except that the legend is still over the graph :( – José Carlos Dec 01 '17 at 17:34
  • Please avoid answering duplicate questions. It just clutters the site, and encourages more time-wasting bad questions that haven't bothered to do their own research. Particularly when the duplicate has already been flagged. Better approach is vote to close (once you have enough rep to do so). – dww Dec 01 '17 at 19:05