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 )
4

1 回答 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 并在 Adob​​e Illustrator 或其他工具中进行精细编辑。

于 2014-07-11T03:09:17.567 回答