Error writing conditional regression equation as legend in R
Background
I am trying to get the regression equation through bquote
in a legend. Using version # 1 (see the R code below the picture) of my R-code, I get the screenshot below.
BUT , I need the IF b
(see the R code below the figure) is the "negative" THEN icon to +
be removed. To achieve these criteria, I came up with version # 2 (see the R code below the picture) of my R code.
Question
I am wondering how to fix a bug in my Version # 2 R code (see the R code below the picture)?
Error: unexpected ')' in " bty = "n")"
Version # 1:
plot(1, ty = 'n')
a = .234; b = -.335
legend("center", legend = bquote(bold(Outcome[i] == .(round (a , 2))~ + ~.
(round(b , 2))~"\u00D7"~Predictor[i])), bty = "n")
Version # 2:
legend("center", legend = bquote(bold(Outcome[i] == .(round (a , 2))~
ifelse(.(b > 0), + , "") ~.(round(b , 2))~"\u00D7"~Predictor[i])),
bty = "n")
For me, the simplest fix (and what I usually do in such cases - in your case, I'm not even sure what you can call ifelse
inside bquote
) is to use if-else
outside the call instead of inside :.
plot(1, ty = 'n')
a = .234; b = -.335
if (b > 0) {
legend("center", legend = bquote(bold(Outcome[i] == .(round (a , 2))~ + ~.
(round(b , 2))~"\u00D7"~Predictor[i])), bty = "n")
} else {
legend("center", legend = bquote(bold(Outcome[i] == .(round (a , 2))~ .
(round(b , 2))~"\u00D7"~Predictor[i])), bty = "n")
}
Output:
source to share