r - 在多行中绘制图例标签,包括数学表达式

标签 r plotmath

如何创建包含多行文本的图例标签,后跟单独一行上的数学表达式,以避免下面列出的问题?

已接受的答案 here不适用于图例,据我所知,涉及 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 )

最佳答案

一个选项是

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 等中进行精细编辑。

关于r - 在多行中绘制图例标签,包括数学表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24680514/

相关文章:

R 按位置将字符串列分成两列

list - 从 R 中的 glm 中提取系数

r - 如何连接两个表达式?

r - 在图例中写 x̄(意思是平均值)以及如何防止换行?

r - 如何在绘图标题或标签中的文本下划线?

r - 在组内计算值变化前后的数量,为每个独特的转变生成新变量

r - 只有 .R 文件中的源函数

正则表达式拆分任何不是数字的东西

r - R:如何将两个箱形图彼此相邻放置,并保持两个相同的y范围?

r - 绘图轴标题上的特殊字符和上标