Graphics legend graphics in several lines, including mathematical expressions

How to create legend labels containing several lines of text, and then a mathematical expression on a separate line, avoiding the problems listed below?

The accepted answer here does not work for legends, and as far as I can tell, the alternative answer with bquote also does not work for my case, as shown below.

Playable Code:

# multiple lines in first legend label are not horizontally aligned
# also, entire legend label is not vertically aligned with box
plot( runif(10), runif(10) )
legend( x='top', fill=grey.colors(2), bty='n', ncol=2, inset=.1,
    legend=c( expression( "Hello world\nGoodbye world\n" ~ 64 %/% 8 %/% 8 ),
        'something else' ) )

# both above problems fixed, but math expression doesn't display right
plot( runif(10), runif(10) )
legend( x='top', fill=grey.colors(2), bty='n', ncol=2, inset=.1,
    legend=c( paste( "Hello world\nGoodbye world\n", expression( 64 %/% 8 %/% 8 ) ),
        'something else' ) )

# nested atops within bquote almost works in title, but font size is not uniform
# does not work at all in legend, anyway
plot( runif(10), runif(10) )
label1 = bquote( atop( atop( "Hello world", "Goodbye world" ), 64 %/% 8 %/% 8 ) )
labels = c( label1, 'something else' )
title( label1 )
legend( x='top', fill=grey.colors(2), bty='n', ncol=2, inset=.1, legend=labels )
+4
source share
1 answer

One of the options:

plot( runif(10), runif(10) )
legend( x='top', fill=grey.colors(2), bty='n', ncol=2, inset=.1,
    legend=c( expression( atop("Hello world\nGoodbye world",64 %/% 8 %/% 8) ),
         'something else' ) )

or

plot( runif(10), runif(10) )
legend( x='top', fill=grey.colors(2), bty='n', ncol=2, inset=.1,
    legend=c( expression( atop(atop("Hello world","Goodbye world"),64 %/% 8 %/% 8) ),
         'something else' ) )

R ?plotmath. , PDF, Adobe Illustrator - .

+1

Source: https://habr.com/ru/post/1547930/


All Articles