There are several things that were needed here, but the main one is getting the 3 expressions in a suitable format - simply c
-ing them together doesn't work. For that I create a list of expressions and then sapply
the as.expression
function over that list to get a "vector of expressions".
I use the plotmath function atop
to get a "linebreak"; this places its arguments, one on top of the other.
To get the 0,95
part, which I presume is just 0.95
but in your locale, on my machine I had to use the layout function *
with a literal ","
to juxtapose the 0
and the 95
. If this is just to write a decimal then in other locales that use .
as the decimal separator, this would not be necessary. I suspect that in your locale, you can use the second version below but writing [0,99]
instead of [0.99]
, but I don't know.
If you just put
bquote(atop(foo_0.99^normal ==, .round(bar, 4)))
you'd just get an error:
> bquote(atop(foo_0.99^normal ==, .round(bar, 4)))
Error: unexpected ',' in "bquote(atop(foo_0.99^normal ==,"
This is because the right hand side of the ==
function/operator is missing. As you don't want anything after the ==
(on the current line!), you need to use something that doesn't add extra space but provides a valid right hand side for ==
. Here I use phantom()
which acts as a placeholder but leaves no space (as I supply it no argument). But you can also use == ""
, substituting ""
for phantom()
.
To get the text in the same colour as the line, use the text.col
argument.
Here is the thing in full with all your requests catered for (after a fashion):
test <- 10:1
dummy1 <- 0.004323423
dummy2 <- 0.054
dummy3 <- 0.032
plot(test, 1:10)
## list of expressions
exprs <-
list(bquote(atop(Qua_0 * "," * 99^normal == phantom(), .(round(dummy1, 4)))),
bquote(atop(Qua_0 * "," * 95^normal == phantom(), .(round(dummy2, 4)))),
bquote(atop(Qua_0 * "," * 99^t == phantom(), .(round(dummy3, 4)))))
## fudge to get them as an expression vector
exprs <- sapply(exprs, as.expression)
cols <- c("red", "black", "darkgreen")
legend("topright", legend = exprs, bty = "n", lwd = 2, cex = 1, col = cols,
lty=c(1,3,5), text.col = cols)
It looks like you are using the LaTeX _
to get superscript. In plotmath you need [ ]
.
You'll also note that this is not quite so nice typographically --- there isn't enough space between the legend entries but some space between the elements within a single entry. This is because we are abusing atop
to fake the newline. We can deal with that using the y.intersp
argument of legend
. Increasing it to 2
gives sufficient space.
Combining this, I would do the following to meet all your requirements:
plot(test, 1:10)
## list of expressions
exprs <-
list(bquote(atop(Qua[0.99]^normal == phantom(), .(round(dummy1, 4)))),
bquote(atop(Qua[0.95]^normal == phantom(), .(round(dummy2, 4)))),
bquote(atop(Qua[0.99]^t == phantom(), .(round(dummy3, 4)))))
## fudge to get them as an expression vector
exprs <- sapply(exprs, as.expression)
cols <- c("red", "black", "darkgreen")
legend("topright", legend = exprs, bty = "n", lwd = 2, cex = 1, col = cols,
lty=c(1,3,5), text.col = cols, y.intersp = 2)
This produces:
