6

This is a following up question of this question: How to have a new line in a `bquote` expression used with `text`?

But now I have it in a legend and this seems to change the things.

I tried the following:

test<-c(10:1)
dummy1<-0.004323423
dummy2<-0.054
dummy3<-0.032
plot(test,c(1:10))
legend("topright", 
 legend=c(bquote(Qua_0,99^normal == .(round(dummy1,4))),bquote(Qua_0,95^normal == .(round(dummy2,4))),bquote(Qua_0,99^t == .(round(dummy3,4)))),
 bty = "n",lwd=2, cex=1, col=c("red","black","darkgreen"), lty=c(1,3,5))

So, I want to have

  1. The expression correct, so that the index 0,95 is correctly written and also the power ^ correclty

  2. linebreak after the equal sign

  3. colored text, the same as the lign, so the first would be in red

I tried to implement the answers of the already existing posts but I did not figured it out, atop is also not working.

Community
  • 1
  • 1
Stat Tistician
  • 813
  • 5
  • 17
  • 45

2 Answers2

6

First create a vector of 3 expressions, and use substitute to create the appropriate dummy values. Note that I am using as.expression so that they are not immediately evaluated, and the use of atop for the line break. Then use that vector when calling legend:

v <- c(
 as.expression(substitute(atop(Qua[0.99]^normal == "", dummy), list(dummy=round(dummy1,4)))),
 as.expression(substitute(atop(Qua[0.95]^normal == "", dummy), list(dummy=round(dummy2,4)))),
 as.expression(substitute(atop(Qua[0.99]^t == "", dummy), list(dummy=round(dummy3,4))))
)
cols <- c("red","black","darkgreen")
legend("topright", legend=v, bty = "n",lwd=2, cex=1, col=cols, text.col=cols, lty=c(1,3,5))

The color of the text is set with text.col. enter image description here

If you want to stick to the use of bquote rather than substitute:

 v <- c(
  as.expression(bquote(atop(Qua[0.99]^normal == "", .(round(dummy1,4))))),
  as.expression(bquote(atop(Qua[0.95]^normal == "", .(round(dummy2,4))))),
  as.expression(bquote(atop(Qua[0.99]^t == "", .(round(dummy3,4)))))
 )

To make the normal and 0.99 bold, you can use bold in the expression:

v <- c(
 as.expression(substitute(atop(Qua[bold(0.99)]^bold(normal) == "", dummy), list(dummy=round(dummy1,4)))),
 as.expression(substitute(atop(Qua[bold(0.95)]^bold(normal) == "", dummy), list(dummy=round(dummy2,4)))),
 as.expression(substitute(atop(Qua[bold(0.99)]^bold(t) == "", dummy), list(dummy=round(dummy3,4))))
)

But the 0.99 will not be very bold actually: enter image description here

You can try with text in normal size using textstyle:

v <- c(
 as.expression(substitute(atop(Qua[textstyle(0.99)]^textstyle(normal) == "", dummy), list(dummy=round(dummy1,4)))),
 as.expression(substitute(atop(Qua[textstyle(0.95)]^textstyle(normal) == "", dummy), list(dummy=round(dummy2,4)))),
 as.expression(substitute(atop(Qua[textstyle(0.99)]^textstyle(t) == "", dummy), list(dummy=round(dummy3,4))))
)

and get this: enter image description here

Julián Urbano
  • 8,378
  • 1
  • 30
  • 52
  • ok thanks, but how can I get a linebreak after the equal sign and the text color? – Stat Tistician Mar 28 '13 at 13:36
  • @StatTistician the only way to do this in a legend is to use the `atop` plotmath function. See how this is used in my answer. – Gavin Simpson Mar 28 '13 at 13:42
  • 2
    You can "fix" the spacing the spacing issue between legend elements by adding `y.intersp = 2` to the legend call. – Gavin Simpson Mar 28 '13 at 13:51
  • @GavinSimpson one more question: how can I increase the "normal" and the "0.95"? Increasing cex in legend call increases everything, but I want to increase or write the "normal" and "0.95" more bold? – Stat Tistician Mar 28 '13 at 14:36
  • @caerolus maybe you also know how to increase the "normal" and the "0.95" letters? – Stat Tistician Mar 28 '13 at 14:37
  • 1
    I don't think you can do this directly using plotmath - one option but it is a hack might be to play around with the `textstyle()` and `scriptstyle()` plotmath functions in combination with `cex`. For example, you could put everything **except** "normal" and "0.95" in `scriptstyle()` wrappings, and then use `cex` to increase the overall font size again. The smaller font for the sub- and superscript is the normal convention in mathmathical typsetting, hence this is what `plotmath` does. – Gavin Simpson Mar 28 '13 at 14:42
  • 1
    @StatTistician Added it at the end of the answer – Julián Urbano Mar 28 '13 at 14:51
5

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:

enter image description here

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453