Hi, I am trying to use a legend title that is a bit too long for one line. To try to break the title in two lines, I am using legend (title="Top of Title\nbottom of title", etc. ) R prints the title as two lines, but the top line is outside the legend box. How can I trick the R legend function to make the legend box taller so that it contains the top line? Thanks, Gabriel
Two-line title in R legend
3 messages · Gabriel Toro, David L Carlson, David Winsemius
The top line is not outside the box in this example, but the bottom text line is too close to the bottom. You can turn off drawing the box in legend(). Store the return value of legend() which indicates where the box would have been drawn and use rect() to add it after increasing the dimension you need. In the example I lowered the bottom of the box by .1:
plot(c(0:10), rnorm(11), ylim=c(-3, 3)) pos <- legend(.5, 3, "Top of title\nBottom of title", pch=1,
+ bty="n")
pos
$rect $rect$w [1] 2.558594 $rect$h [1] 0.6723839 $rect$left [1] 0.5 $rect$top [1] 3 $text $text$x [1] 1.0625 $text$y [1] 2.578779
xleft <- pos$rect[["left"]] ytop <- pos$rect[["top"]] ybottom <- ytop - pos$rect[["h"]] xright <- xleft + pos$rect[["w"]] rect(xleft, ybottom-.1, xright, ytop)
---------------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77843-4352
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- project.org] On Behalf Of Gabriel Toro Sent: Sunday, November 11, 2012 9:13 PM To: r-help at R-project.org Subject: [R] FW: Two-line title in R legend Hi, I am trying to use a legend title that is a bit too long for one line. To try to break the title in two lines, I am using legend (title="Top of Title\nbottom of title", etc. ) R prints the title as two lines, but the top line is outside the legend box. How can I trick the R legend function to make the legend box taller so that it contains the top line? Thanks, Gabriel
______________________________________________ R-help at r-project.org mailing list 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.
On Nov 11, 2012, at 7:13 PM, Gabriel Toro wrote:
Hi, I am trying to use a legend title that is a bit too long for one line. To try to break the title in two lines, I am using legend (title="Top of Title\nbottom of title", etc. ) R prints the title as two lines, but the top line is outside the legend box. How can I trick the R legend function to make the legend box taller so that it contains the top line?
You should include an example with dummy data if you wnat tested solutions. You can try:
legend (title=atop("Top of Title","bottom of title"), ... )
Plotmath expression are not allowed to have "\n" in them, as stated in the help page.
David Winsemius, MD Alameda, CA, USA