为什么我的qqnorm图不能作为函数的一部分或括号内运行?

时间:2019-03-19 23:50:03

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

我正试图告诉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不能作为高亮显示的块同时正确地读取它?

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

1 个答案:

答案 0 :(得分:0)

由于@MrFlick现在可以工作了。正如他解释的那样:

  

行尾不应有加号(+)。 qqnorm通常使用>基础图形(plot()和axis()等也是如此。您不要将+与基础> graphics一起使用,仅用于ggplot绘图。该错误消息与pch> parameter无关,即只是在此处截断了直线。大约是两个qqnorm()调用之间的+>。

我删除了(+),尽管alpha值仍然存在一些错误,但它现在确实是一个块。