[R] \n and italic() in legend()

William Michels wjm1 at caa.columbia.edu
Fri Dec 30 06:12:36 CET 2016


Hi Marc, I think it would be wrong to leave readers with the
impression that it's somehow improper to use c() in drawing a legend,
because in fact, it works so well. What doesn't work so well is mixing
expression() calls with escaped characters like "\n" (or "\r"), and
that's probably due to expression() using plotmath() and
as.graphicsAnnot() to draw text.

Maybe the take-home lesson is to not mix expression() and escaped
characters in a legend? If no expression() call is present, "\n" works
fine:

## legend: two lines per variable, no expression() call
plot(1, 1)
v1 <- c("some great text\nhere")
v2 <- c("some more great\ntext here")
legend("topright", legend=c(v1, v2), y.intersp = 1.5, bty="n",
lty=c(1, 1), lwd = c(2, 2), col=c("black", "red"))

If an expression() is present, every time legend() encounters a new
line (via either a compound expression, or via "\n"), it treats it as
a location to display a new variable. However, taking advantage of
plotmath(), you can simply use scriptstyle() or even
scriptscriptstyle(), to draw smaller text on one line:

## legend: expression() call /w single line per variable
plot(1, 1)
v1 <- expression(italic("p")*"-value based on "*italic("t")*"-test")
v2 <- expression(italic("w")*"-value for A and B identical models")
legend("topright", legend=c(v1, v2), y.intersp = 1.0, bty="n",
lty=c(1, 1), lwd = c(2,2), col=c("black", "red"))

## legend: expression() call /w two lines per variable
## (note lty, lwd, and col correction)
plot(1, 1)
v1 <- expression(italic("p")*"-value", "based on "*italic("t")*"-test")
v2 <- expression(italic("w")*"-value", "for A and B identical models")
legend("topright", legend=c(v1, v2), y.intersp = 1.0, bty="n",
lty=c(1, 0, 1, 0), lwd = c(2, 0, 2, 0), col=c("black", "", "red", ""))

## legend: expression() call /w single line per variable,
## smaller script
plot(1, 1)
v1 <- expression(scriptstyle(bold(italic("p")*"-value based on
"*italic("t")*"-test")))
v2 <- expression(scriptstyle(bold(italic("w")*"-value for A and B
identical models")))
legend("topright", legend=c(v1,v2), y.intersp = 1.0, bty="n", lty=c(1,
1), lwd = c(2,2), col=c("black", "red"))

## legend: expression() call /w single line per variable,
## even smaller script
plot(1, 1)
v1 <- expression(scriptscriptstyle(bold(italic("p")*"-value based on
"*italic("t")*"-test")))
v2 <- expression(scriptscriptstyle(bold(italic("w")*"-value for A and
B identical models")))
legend("topright", legend=c(v1,v2), y.intersp = 1.0, bty="n", lty=c(1,
1), lwd = c(2,2), col=c("black", "red"))

I'm loathe to call your initial finding of  'an italicized character
jumping to the second line when used in conjunction with "\n" ' as a
bug, but maybe others can chime in as to why that happens.

HTH,

Bill
William Michels, Ph.D.



On Thu, Dec 29, 2016 at 1:45 PM, Marc Girondot <marc_grt at yahoo.fr> wrote:
> Hi,
> Thanks a lot to Duncan Mackay for the trick using atop() [but the legends
> are centered and not left aligned] and also for the suggestion of William
> Michels to use simply ",". However this last solution prevents to use
> several legends.
>
> Here is a solution to allow both return within a legend and several legends:
> plot(1, 1)
> v1 <- c(expression(italic("p")*"-value"), expression("based on
> "*italic("t")*"-test"))
> v2 <- c(expression(italic("w")*"-value for A"), expression("and B identical
> models"))
> legend("topright", legend=c(v1, v2), lty=c(1, 0, 1, 0), y.intersp = 1,
> bty="n", col=c("black", "", "red", ""))
>
> Thanks again
>
> Marc
>
>
> Le 29/12/2016 à 10:54, Duncan Mackay a écrit :
>>
>> Hi Marc
>>
>> Try atop
>>
>> plot(1, 1)
>> v1 <- expression(atop(italic("p")*"-value","based on
>> "*italic("t")*"-test"))
>> legend("topright", legend=v1, y.intersp = 3, bty="n")
>>
>>
>> Regards
>>
>> Duncan
>>
>> Duncan Mackay
>> Department of Agronomy and Soil Science
>> University of New England
>> Armidale NSW 2351
>> Email: home: mackay at northnet.com.au
>> -----Original Message-----
>> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Marc
>> Girondot via R-help
>> Sent: Thursday, 29 December 2016 20:35
>> To: R-help Mailing List
>> Subject: [R] \n and italic() in legend()
>>
>> Hi everyone,
>>
>> Could someone help me to get both \n (return) and italic() in a legend.
>> Here is a little example showing what I would like (but without the
>> italic) and second what I get:
>>
>> plot(1, 1)
>> v1 <- "p-value\nbased on t-test"
>> legend("topright", legend=v1, y.intersp = 3, bty="n")
>>
>> plot(1, 1)
>> v1 <- expression(italic("p")*"-value\nbased on "*italic("t")*"-test")
>> legend("topright", legend=v1, y.intersp = 3, bty="n")
>>
>> The second one shows :
>>
>> -value
>> pbased on t-test
>>
>> rather than the expected:
>>
>> p-value
>> based on t-test
>>
>> Thanks a lot,
>>
>> Marc
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>



More information about the R-help mailing list