将plotmath字符串替换为表达式

时间:2014-03-26 14:38:45

标签: r graphics plot expression plotmath

我无法在网站上的大量相似但不完全相同的问题中找到答案。假设我在变量中有一个字符串,它是我想要使用的plotmath表达式的直接表示,例如"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) )

enter image description here

#  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 ) ) ) )

1 个答案:

答案 0 :(得分:3)

在这里,parse可以解决问题:

mn <- "R^2"

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

enter image description here