相关疑难解决方法(0)

bquote:如何包含保存为字符串对象的表达式?

我的目标是用最佳拟合线的斜率注释绘图并标记斜率的单位,其中标签保存为单独的字符串对象。我无法弄清楚如何bquote()将字符串对象转换为表达式,并将其与其他评估语句结合使用。

演示:

# example data:
x <- c(1:20)                   # x units: time
y <-  x * rnorm(20, 10, 2)     # y units: length per time

unit.label <- "L%.%T^-2"       # label for slope of best fit line 
lm1 <- summary(lm(y ~ x))

plot(y ~ x)
Run Code Online (Sandbox Code Playgroud)

当我尝试注释情节时会出现问题。我可以得到 bquote() 来显示斜率:

text(median(x), min(y), bquote(slope: .(round(lm1$coefficients[2], 2))) )
Run Code Online (Sandbox Code Playgroud)

我还可以bquote()显示斜率的单位:

plot(y ~ x)
text(median(x), min(y), bquote(.(parse(text = unit.label))) )
Run Code Online (Sandbox Code Playgroud)

但我无法将标签和斜率合并为一个bquote()语句:

plot(y ~ x)
text(median(x), min(y), bquote(slope: .(round(lm1$coefficients[2], 2)) 
.(parse(text = …
Run Code Online (Sandbox Code Playgroud)

string expression r

5
推荐指数
1
解决办法
6357
查看次数

标签 统计

expression ×1

r ×1

string ×1