我试图告诉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不能作为高亮显示的块将它们全部同时读取为一个函数?
任何帮助将非常感激,
-谢谢。
最佳答案
感谢@MrFlick,它现在可以工作了。正如他解释的那样:
There should not be pluses (+) at the end of your lines. qqnorm is normally uses >base graphics (as does plot() and axis() and the like. You don't use + with base >graphics, that's only for ggplot plots. That error message is not about the pch >parameter, that's just here it cut off the very line line. It's about the + >between the two qqnorm() calls.
我删除了(+),尽管alpha值仍然存在一些错误,但它现在确实作为一个块运行。
关于r - 为什么我的qqnorm图不能作为函数的一部分或括号内运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55251596/