【问题标题】:Why cant my qqnorm graph run as part of a function or within brackets?为什么我的 qqnorm 图不能作为函数的一部分或在括号内运行?
【发布时间】:2019-08-10 14:32:27
【问题描述】:

我试图告诉 R 使用“{ }”括号一次运行多行。我以前成功地做到了这一点。我试图通过在一个函数中定义多个图来一次生成多个图。

例如:

qqnorm(residuals(LM_auto), col="black", fg="blue", pch= 21, bg=col_1, alpha = .7, sub = shapiro_output, xlab = "Shapiro Results Below:")+qqline(residuals(LM_auto))
plot(density(residuals(LM_auto)), col="black", fg="blue", bg=col_1) + polygon(density(residuals(LM_auto)), col=col_1)+
plot(residuals(LM_auto)~fitted(LM_auto), col="black", fg="blue", pch= 21, bg=col_1, alpha = .7, main="Summary Output-------------->")+
abline(h = c(100, 10, 2, 0, -2, -10, -100), col = c("yellow", "orange", "blue", "black", "blue", "orange", "yellow"), lty = c(5,4,2,1,2,4,5))+
plot(residuals(LM_auto)~xparameter, xlab=X_variable_title, col=col_1, fg="blue", pch= 21, bg=col_1, alpha = .7, main=summary_output, cex.main=.6)+
abline(h = c(100, 10, 2, 0, -2, -10, -100), col = c("yellow", "orange", "blue", "black", "blue", "orange", "yellow"), lty = c(5,4,2,1,2,4,5))'

所有这些行都有效,即使在突出显示并作为一个块运行时也是如此。但是,如果我尝试将它们放入括号中,或者将块定义为函数,它们将不会运行(除了生成 qqplot),并且 R 返回一组错误。

{
qqnorm(residuals(LM_auto), col="black", fg="blue", pch= 21, bg=col_1, alpha = .7, sub = shapiro_output, xlab = "Shapiro Results Below:")+qqline(residuals(LM_auto))
plot(density(residuals(LM_auto)), col="black", fg="blue", bg=col_1) + polygon(density(residuals(LM_auto)), col=col_1)+
plot(residuals(LM_auto)~fitted(LM_auto), col="black", fg="blue", pch= 21, bg=col_1, alpha = .7, main="Summary Output-------------->")+
abline(h = c(100, 10, 2, 0, -2, -10, -100), col = c("yellow", "orange", "blue", "black", "blue", "orange", "yellow"), lty = c(5,4,2,1,2,4,5))+
plot(residuals(LM_auto)~xparameter, xlab=X_variable_title, col=col_1, fg="blue", pch= 21, bg=col_1, alpha = .7, main=summary_output, cex.main=.6)+
abline(h = c(100, 10, 2, 0, -2, -10, -100), col = c("yellow", "orange", "blue", "black", "blue", "orange", "yellow"), lty = c(5,4,2,1,2,4,5))
}

错误:

Error in qqnorm(residuals(LM_auto), col = "black", fg = "blue", pch = as.numeric(21),  : 
non-numeric argument to binary operator

In addition: Warning messages:

1: In plot.window(...) : "alpha" is not a graphical parameter

2: In plot.xy(xy, type, ...) : "alpha" is not a graphical parameter

3: In axis(side = side, at = at, labels = labels, ...) : "alpha" is not a graphical parameter

4: In axis(side = side, at = at, labels = labels, ...) : "alpha" is not a graphical parameter

5: In box(...) : "alpha" is not a graphical parameter

6: In title(...) : "alpha" is not a graphical parameter'

这似乎是 qqnorm 函数的错误,因为只要删除该行,我就可以在括号内运行我的代码。

为什么 21 不被识别为数字?或者这真的是问题吗?我试过了:

pch=as.numeric(21)

但这仍然不起作用。

当 R 可以将其作为突出显示的块时,为什么不能将它们作为函数一起正确读取?

任何帮助将不胜感激, -谢谢。

【问题讨论】:

  • 行尾不应有加号 (+)。 qqnorm 通常使用基本图形(plot()axis() 等。您不要将 + 与基本图形一起使用,这仅适用于 ggplot 图。该错误消息与 pch 无关参数,就在这里,它切断了这一行。它是关于两个qqnorm() 调用之间的+。将来,如果您提供带有示例输入数据的reproducible example(所有变量定义)

标签: r function compiler-errors syntax-error curly-braces


【解决方案1】:

感谢@MrFlick,它现在可以工作了。正如他解释的那样:

行尾不应有加号 (+)。 qqnorm 通常使用 >base graphics(plot() 和 axis() 等。你不使用 + 与 base >graphics,这仅适用于 ggplot 图。该错误消息与 pch >parameter 无关,那是就在这里,它切断了这条线。它是关于两个 qqnorm() 调用之间的 + >。

我删除了 (+),虽然 alpha 值仍然存在一些错误,但它现在作为一个块运行。

【讨论】:

    猜你喜欢
    • 2021-05-04
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    • 2017-11-07
    • 1970-01-01
    • 1970-01-01
    • 2020-07-16
    • 1970-01-01
    相关资源
    最近更新 更多