0

I am attempting to tell R to run multiple lines at a time using "{ }" brackets. I have done this with success before. I am attempting to generate multiple plots at a time by defining several plots in one function.

for example:

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))'

All of these lines work, even when highlighted and run as a block. However, if I attempt to put them into brackets, or define the block as a function, they will not run (aside from generating the qqplot), and R returns a set of errors.

{
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))
}

errors:

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'

It seems to be an error with the qqnorm function, as I can run my code within brackets as long as that line is removed.

Why is 21 not recognized as numeric? Or is that really the issue? I have tried it with:

pch=as.numeric(21)

But this still does not work.

Why can't R properly read this all together as a function when it can do so as a highlighted block?

Any help would be much appreciated, -Thanks.

  • There should not be pluses (`+`) at the end of your lines. `qqnorm` is normally uses base graphocs (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. In the future it's easier to help you if you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data (all your variables defined) – MrFlick Mar 20 '19 at 03:09

1 Answers1

0

thanks to @MrFlick it is now working. As he explained:

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.

I removed the (+)s, and while there are still some errors with alpha values, it does now run as a block.