在绘图标签中组合 paste() 和 expression() 函数 [英] Combining paste() and expression() functions in plot labels
问题描述
考虑这个简单的例子:
labNames <- c('xLab','yLabl')
plot(c(1:10),xlab=expression(paste(labName[1], x^2)),ylab=expression(paste(labName[2], y^2)))
我想要的是由变量labName"定义的字符条目,'xLab' 或 'yLab' 出现在由 expression() 定义的 X^2 或 y^2 旁边.实际上,带有下标的实际文本labName"与上标表达式相连.
What I want is for the character entry defined by the variable 'labName, 'xLab' or 'yLab' to appear next to the X^2 or y^2 defined by the expression(). As it is, the actual text 'labName' with a subscript is joined to the superscripted expression.
有什么想法吗?
推荐答案
@Aaron 的替代解决方案是 bquote()
函数.我们需要提供一个有效的 R 表达式,例如在这种情况下 LABEL ~ x^2
,其中 LABEL
是您要从向量 labNames 分配的字符串代码>.
bquote
对包裹在 .( )
中的表达式中的 R 代码求值,并将结果代入表达式中.
An alternative solution to that of @Aaron is the bquote()
function. We need to supply a valid R expression, in this case LABEL ~ x^2
for example, where LABEL
is the string you want to assign from the vector labNames
. bquote
evaluates R code within the expression wrapped in .( )
and subsitutes the result into the expression.
这是一个例子:
labNames <- c('xLab','yLab')
xlab <- bquote(.(labNames[1]) ~ x^2)
ylab <- bquote(.(labNames[2]) ~ y^2)
plot(c(1:10), xlab = xlab, ylab = ylab)
(注意~
只是加了一点空格,如果你不想要空格,用*
代替,表达式的两部分就是并列.)
(Note the ~
just adds a bit of spacing, if you don't want the space, replace it with *
and the two parts of the expression will be juxtaposed.)
这篇关于在绘图标签中组合 paste() 和 expression() 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!