“高度"必须是向量或矩阵.条码错误 [英] 'height' must be a vector or a matrix. barplot error
本文介绍了“高度"必须是向量或矩阵.条码错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个简单的条形图,但是我一直收到错误消息
I am trying to create a simple bar chart, but I keep receiving the error message
'height' must be a vector or a matrix
我一直在尝试的barplot函数是
The barplot function I have been trying is
barplot(data, xlab="Percentage", ylab="Proportion")
我已经输入了csv,数据如下:
I have inputted my csv, and the data looks as follows:
34.88372093 0.00029997
35.07751938 0.00019998
35.27131783 0.00029997
35.46511628 0.00029997
35.65891473 0.00069993
35.85271318 0.00069993
36.04651163 0.00049995
36.24031008 0.0009999
36.43410853 0.00189981
...
我在哪里错了?
提前谢谢!
dput(head(data)) outputs:
structure(list(V1 = c(34.88372093, 35.07751938, 35.27131783,
35.46511628, 35.65891473, 35.85271318), V2 = c(0.00029997, 0.00019998,
0.00029997, 0.00029997, 0.00069993, 0.00069993)), .Names = c("V1",
"V2"), row.names = c(NA, 6L), class = "data.frame")
和barplot(as.matrix(data))
生成了一个图表,其中所有数据都在一个条形图上,而每条数据都在一个单独的条形图上.
and barplot(as.matrix(data))
produced a chart with all the data one bar as opposed to each piece of data on a separate bar.
推荐答案
您可以指定要绘制的两个变量,而不是像这样传递整个数据框:
You can specify the two variables you want to plot rather than passing the whole data frame, like so:
data <- structure(list(V1 = c(34.88372093, 35.07751938, 35.27131783, 35.46511628, 35.65891473, 35.85271318),
V2 = c(0.00029997, 0.00019998, 0.00029997, 0.00029997, 0.00069993, 0.00069993)),
.Names = c("V1", "V2"), row.names = c(NA, 6L), class = "data.frame")
barplot(data$V2, data$V1, xlab="Percentage", ylab="Proportion")
或者,您可以使用ggplot
来做到这一点:
Alternatively, you can use ggplot
to do this:
library(ggplot2)
ggplot(data, aes(x=V1, y=V2)) + geom_bar(stat="identity") +
labs(x="Percentage", y="Proportion")
这篇关于“高度"必须是向量或矩阵.条码错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文