轴标签与表达式的 R 对齐 [英] R alignment of axis labels with expressions

查看:31
本文介绍了轴标签与表达式的 R 对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制一个变量名称及其符号.因为某些变量的名称很长,所以我试图将换行符与轴标签混合在一起.这会导致对齐中发生有趣的事情:

I would like to plot a variable name and its symbol. Because some variables have long names, I'm attempting to mix line breaks with the axis labels. This causes funny things to happen in the alignments:

par(mar=c(1,12,1,1))

plot( y=1:6, 1:6, yaxt="n", ylim=c(1,6), ylab="" )

axis(2, at=6:1, las=1,
     labels=c(
  expression( paste( 'Variable, ',rho, sep="" ) ),
  expression( paste( 'Another variable,\n',alpha, sep="" ) ),
  expression( paste( 'Also\nvariable,\n',beta, sep="" ) ),
  expression( paste( 'This\nvariable too, ',Gamma, sep="" ) ),
  expression( paste( 'Verybigname\nvariable,',C[zeta], sep="" ) ),
  expression( paste( 'Verybigname\nvariable,',C[zeta],"moretext\n",C[delta], sep="" ) ) ))

第一行(从顶部开始)是我期望其他行在某种程度上的样子,它在轴旁边是合理的.

The first line (from the top) is what I expected the others to look like in some way, which is justified next to the axis.

其余的行都显示了一个基本问题,即变量名称相对于表达式的第二个元素是合理的,而不是相对于轴.这在最后一行变得特别奇怪,其中在变量名和它的符号之间插入了一个大空格.

The rest of the lines all show the basic problem that the variable name is justified with respect to the second element of the expression, not to the axis. This gets particularly weird in the last line where a large space is inserted between the variable name and its symbol.

理想情况下,我希望标签右对齐,并以刻度线为中心.

Ideally, I would like the labels right justified, and centered on the tick mark.

推荐答案

这是我使用 text 并手动"定位在侧面的最佳效果.(许多使用 'hadj' 和 'padj' 的轴的努力都失败了.)为了尝试在文本值下获取希腊语绘图,我需要使用 atop 函数.这反过来需要使用 phantom.我主要使用带有 ~* 的纯 plotmath 作为连接器,而不是依赖 paste,但在这里你使用 "\n" 确实完成了换行(我觉得很惊讶).顺便说一下,plotmath paste 函数实际上并不接受 sep 参数.

This is the best I can do with using text with positioning on the side "by hand". (Many efforts with axis using 'hadj' and 'padj' failed.) To try to get the plotmath greeks under the text values, I needed to use the atop function. This in turn required using phantom. I mostly use pure plotmath with ~ and * as connectors rather than relying on paste, but here your use of "\n" does acomplish a line break (which I found surprising). By the way, the plotmath paste function does not actually accept a sep argument.

par(mar=c(1,12,1,1))
plot( y=1:6, 1:6, yaxt="n", ylim=c(1,6), ylab="" )

axis(2, at=6:1, las=1,
     labels=rep("",6 ))
text( 0.5, 6:1- c(0,0,.2,.1,.1,.1), c(
   expression( Variable~~rho ) ,
   expression( atop(~~Another~variable, phantom("Another~variable")~alpha)  ),
   expression( atop(paste( '        Also\n   variable') ,phantom("variable")~beta ) ),
   expression( atop( paste( '            This\nvariable too') ,phantom("           ")~Gamma ) ),
   expression( atop( paste( '   Verybigname\n            variable,'),phantom("  Verybigname")~C[zeta] ) ),
   expression(  paste( '   Verybigname\nvariable,',C[zeta],"moretext\n",C[delta] ) ) ), 
xpd=TRUE ,adj=1)

这篇关于轴标签与表达式的 R 对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆