多行显示包含数学表达式的图例标签

5

如何创建包含多行文本和数学表达式的图例标签,使其在单独的一行上,同时避免以下问题?

这里接受的答案对于图例不起作用,而我所知道的,涉及bquote的备选答案也不适用于我的情况,如下所示。

可重现代码:

# 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 )
1个回答

1
一个选项是:
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' ) )

或者

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或其他软件中进行精细编辑。

嗯,是的,我得出了同样的结论。提出的解决方案接近完美,但还不够完美——第一个解决方案没有像数学表达式那样对齐文本字符串,而第二个解决方案使文本大小比数学表达式大小小。我想我必须手动编辑。 - baixiwei

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接