1

我无法在网站上的众多类似但不完全相同的问题中找到答案。假设我在变量中有一个字符串,它是我想要使用的绘图表达式的直接表示,例如"R^2". 我怎样才能将它替换为一个expression所以它被呈现为 plotmath?我会疯狂地尝试每一种组合,但我无法让它发挥作用......

#  What I want...
mn <- "R^2"
#  Using expression directly to show what I'd like to get
plot( 1 , main = expression(R^2) )

在此处输入图像描述

#  What I tried to substitute the value of 'mn' into an expression
plot( 1 , main = expression( mn ) )
plot( 1 , main = bquote( .(mn) ) )
plot( 1 , main = bquote( paste( .(mn) ) ) )
plot( 1 , main = do.call( expression , list( mn ) ) )
plot( 1 , main = do.call( expression , list( bquote( .(mn) ) ) ) )
plot( 1 , main = do.call( expression , list( bquote( paste( .(mn) ) ) ) ) ) #getting silly
plot( 1 , main = deparse( substitute( mn , list( mn = mn ) ) ) )
4

1 回答 1

3

在这里,parse将做的伎俩:

mn <- "R^2"

plot(1, main = parse(text = mn))

在此处输入图像描述

于 2014-03-26T14:50:07.933 回答