-0.01 * height中的错误:二元运算符的非数字参数

时间:2016-04-24 01:01:16

标签: r plot bar-chart

我试图在CSV文件中为我的数据创建条形图。我使用了以下代码 [based on this tutorial]

topics <- read.csv("test.csv")
data <- structure(list(T1= as.numeric(topics$T1), 
                       T2 = as.numeric(topics$T2),
                       T3 = as.numeric(topics$T3), 
                       T4 = as.numeric(topics$T4),
                       T5 = as.numeric(topics$T5),
                       T6 = as.numeric(topics$T6),
                       .Names = c("T1","T2","T3","T4","T5","T6"),
                       class = "data.frame"),
                       row.names = c(NA, 6L))
attach(data)
print(data)
dput(head(data))
colours <- c("red", "orange", "blue", "yellow", "green","black")
barplot(as.matrix(data), main="My Barchart", ylab = "Numbers", cex.lab = 1.5, cex.main = 1.4, beside=TRUE, col=colours)
legend("topleft", c("First","Second","Third","Fourth","Fifth","Sixth"), cex=1.3, bty="n", fill=colours)

我无法解决此错误:

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

dput(head(data))表示数据已成功读取!!但错误仍然存​​在。

1 个答案:

答案 0 :(得分:1)

正如Richard Scriven正确建议的那样,您必须将as.matrix(data)替换为as.matrix(as.data.frame(data))

你也可以尝试这个代码:

colours <- c("red", "orange", "blue", "yellow", "green","black")
data2 <- data.frame(data)
barplot(as.matrix(data2), main="My Barchart", ylab = "Numbers", cex.lab = 1.5, cex.main = 1.4, beside=TRUE, col=colours)
legend("topleft", c("First","Second","Third","Fourth","Fifth","Sixth"), cex=1.3, bty="n", fill=colours)