我有大部分s3构造函数赋值工作,感谢stackoverflow贡献者,但现在我在创建信息的条形图时遇到了问题:
我的构造函数和变量目前是:
# Create constructor
ChlorReads <- function(theid, thename, thegender, theldl, thehdl, thetrigl) {
x <- list(id=theid, name=thename, gender=thegender, ldl=theldl, hdl=thehdl, trigl=thetrigl)
class(x) = "Patient"
return(x)
}
# Prints the patient information (one item in the list)
print.Patient = function(x, ...){
cat("ID: ", x$id, " Name: ", x$name, " Gender: ", x$gender,
"\nLDL: ", x$ldl, " HDL: ", x$hdl, " Triglycerides: ", x$trigl,"\n", sep="")
}
# Input p1
p1 <- ChlorReads(9876, "Virgil", "M", 248, 45, 148)
我想最终绘制(p1),所以我试图创建另一个函数,允许我创建一个如下图所示的表:
我目前有:
plot.Patient = function(x, ...) {
counts <- table(x$ldl,x$hdl,x$trigl)
barplot(counts, main="Chloresterol Readings")
}
但桌子不起作用。我收到以下错误:barplot.default中的错误(计数,主要=&#34; Chloresterol读数&#34;): &#39;高度&#39;必须是向量或矩阵
答案 0 :(得分:1)
输入barplot函数的值(高度)必须是矢量或矩阵形式。这意味着您的对象计数&#39;是不正确的。请参阅barplot文档: https://stat.ethz.ch/R-manual/R-devel/library/graphics/html/barplot.html
答案 1 :(得分:0)
对于遇到此问题的任何人,下面的代码创建的输出类似于上面发布的示例图像。
plot.Patient = function(x, ...) {
counts <- c(x$ldl,x$hdl,x$trigl)
barplot(counts, main="Chloresterol Readings", xlab = c("Name: ", x$name), names.arg = c("LDL", "HDL", "Triglycerides"))
}