R:小节高度必须是向量或矩阵 [英] R: Barplot height must be a vector or matrix

查看:52
本文介绍了R:小节高度必须是向量或矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用成组的bar创建一个barplot,但是出现此错误:

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

最后,称为"data"的数据帧的内容为...

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

我做错了什么?

推荐答案

我认为问题可能是您正在尝试将DataFrame作为height参数传递.这仅应采用向量或矩阵.您可以尝试这样的事情:

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

希望这对您有所帮助.

这篇关于R:小节高度必须是向量或矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆