Search code examples
rplotexpressionlegendplotmath

Advanced legend in R: Plot


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.


Solution

  • 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