First time R user trying to create a simple bar chart, but I keep receiving the error message
第一次R用户尝试创建一个简单的条形图,但是我一直收到错误消息
'height' must be a vector or a matrix
The barplot function I have been trying is
我一直在尝试的barplot函数是
barplot(data, xlab="Percentage", ylab="Proportion")
I have inputted my csv, and the data looks as follows:
我已经输入了csv,数据如下:
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
...
Where am I going wrong here?
我哪里出错了?
Thanks in advance!
提前谢谢!
EDIT:
编辑:
dput(head(data)) outputs:
dput(头(数据))输出:
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")
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.
barplot(as.matrix(data)))生成了一个包含所有数据的图表,每个数据块位于一个单独的条形图上。
1 个解决方案
#1
6
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")
Alternatively, you can use ggplot
to do this:
或者,您可以使用ggplot完成以下操作:
library(ggplot2)
ggplot(data, aes(x=V1, y=V2)) + geom_bar(stat="identity") +
labs(x="Percentage", y="Proportion")
#1
6
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")
Alternatively, you can use ggplot
to do this:
或者,您可以使用ggplot完成以下操作:
library(ggplot2)
ggplot(data, aes(x=V1, y=V2)) + geom_bar(stat="identity") +
labs(x="Percentage", y="Proportion")