本文介绍了在绘图标签中组合paste()和expression()函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下简单示例:

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.

有什么想法吗?

推荐答案

@ca>函数是@Aaron的另一种解决方案.我们需要提供一个有效的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()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 23:06