【问题标题】:non-numeric argument to binary operator in the legend function图例函数中二元运算符的非数字参数
【发布时间】:2021-02-23 19:14:34
【问题描述】:

我有点困惑,为什么我的代码中出现“二进制运算符的非数字参数”错误。下面提供了完整的代码,我只想在其中创建几个图和一个图例。代码工作得很好,可以毫无问题地创建情节。但是,一旦我使用图例函数运行代码,我就会得到可怕的“二进制运算符的非数字参数”。这很奇怪,因为我仍然得到一个带有图例的图,但是我无法使用输出创建 PDF 文件,因为我在 R markdown 中运行它。

关于是什么导致图例函数中的错误的任何线索?

install.packages("faraway")
require(faraway)
mod_tumor1=glm(cbind(aflatoxin$tumor, aflatoxin$total-aflatoxin$tumor) ~ aflatoxin$dose, family=binomial)
mod_tumor2=glm(cbind(aflatoxin$tumor, aflatoxin$total-aflatoxin$tumor) ~ log(aflatoxin$dose+1), family=binomial)
mod_tumor3=glm(cbind(aflatoxin$tumor, aflatoxin$total-aflatoxin$tumor) ~ sqrt(aflatoxin$dose), family=binomial)
x=seq(min(aflatoxin$dose),max(aflatoxin$dose),length=200) 
plot(aflatoxin$dose,aflatoxin$p_hat, type="p", pch=20, main="Logistic Regression Models",
     col=1, xlab = "Dose", ylab= "Percentage of Tumors")+
lines(x,ilogit(mod_tumor1$coef[1]+mod_tumor1$coef[2]*x),lwd=1,col=4)+
lines(x,ilogit(mod_tumor2$coef[1]+mod_tumor2$coef[2]*log(x+1)),lwd=1,col=2)+
lines(x,ilogit(mod_tumor3$coef[[1]]+mod_tumor3$coef[[2]]*sqrt(x)),lwd=1,col=3)+         
legend("bottomright",c("dose","log(dose+1)","sqrt(dose)"),lwd=1,col=c(4,2,3))

非常感谢

【问题讨论】:

  • 为什么在绘图线的末端有+(加号)?您不能将+plot()lines() 等基本图形函数一起使用。这看起来更像是如何使用 ggplot2 函数。
  • @MrFlick,哇,这对我来说相当愚蠢,但我完全忽略了这一点。这实际上解决了这个问题。谢谢。

标签: r r-markdown


【解决方案1】:

感谢评论,只需从代码中删除“+”号即可解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-23
    • 1970-01-01
    • 2018-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-14
    相关资源
    最近更新 更多