bquote:如何包含一个保存为字符串对象的表达式? [英] bquote: How to include an expression saved as a string object?

查看:295
本文介绍了bquote:如何包含一个保存为字符串对象的表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



演示:

 #示例数据:
x< - c(1: 20)#x units:time
y< - x * rnorm(20,10,2)#y units:每次时间的长度

unit.label < - L%。% T ^ -2#最适合线斜率的标签
lm1 < - summary(lm(y〜x))

plot(y〜x)

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

  text(median(x),min(y),bquote :(圆(lm1 $系数[2],2))))

我也可以得到 bquote()显示斜率单位:

  plot(y〜 x)
text(median(x),min(y),bquote(。(parse(text = unit.label))))

但是我无法将标签和斜率组合成单个 bquote()语句:

  plot(y〜x)
文本(中位数(x),最小(y),bquote(斜率:(圆(lm1 $系数[2],2))
。(parse(text = unit.label))))
#错误:text(median(x),min(y) (斜率:
#。(圆(lm1 $系数[2],2))

使用 paste(),单位标签与斜率一起显示,但标签不会被视为表达式:

  plot(y〜x)
文本(中位数(x),最小(y),bquote(斜率: lm1 $系数[2],2),
as.expression(unit.label))))

我哪里错了?在我的bquote命令中,这是一个简单的语法问题吗?
感谢任何建议!

解决方案

1)解析char字符串创建字符串期望(确保它代表在R中语法有效的表达式),然后解析它。这里 main_s 是字符串:

  fm < -  lm y〜x)

main_s< - paste(slope:,round(coef(fm)[2],2),〜,unit.label)
plot 0,main = parse(text = main_s))

语句设置 main_s 可以替换为以下 sprintf 语句,这可以说更可读:

  main_s<  -  sprintf(slope:%.2f〜%s,coef(fm)[2],unit.label)
/ pre>

2)bquote 上面可能是最直接的处理方法,但使用 bquote 尝试这个,其中 unit.label_c 是一个调用对象, fm 如上所述:

  unit.label_c<  -  parse(text = unit.label)[[1]] 
plot(0 ,main = bquote(slope:。(round(coef(fm)[2],2))〜。(unit.label_c)))

在这两种情况下,我们都可以得到:





UPODATE 已添加2)。还有一些改进和修正。


My goal is to annotate a plot with the slope of a best-fit line and label the units of the slope, where the label is saved as a separate string object. I'm having trouble figuring out how to get bquote() to convert a string object into an expression, and combine it with other evaluated statements.

A demonstration:

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

The problem occurs when I try to annotate the plot. I can get bquote() to display the slope:

text(median(x), min(y), bquote(slope: .(round(lm1$coefficients[2], 2))) )

I can also get bquote() to show the slope's units:

plot(y ~ x)
text(median(x), min(y), bquote(.(parse(text = unit.label))) )

But I'm unable to combine the label and the slope into a single bquote() statement:

plot(y ~ x)
text(median(x), min(y), bquote(slope: .(round(lm1$coefficients[2], 2)) 
.(parse(text = unit.label))) ) 
# Error: unexpected symbol in "text(median(x), min(y), bquote(slope: 
# .(round(lm1$coefficients[2], 2)) ."

Using paste(), the unit label appears along with the slope, but the label isn't read as an expression:

plot(y ~ x)
text(median(x), min(y), bquote(slope: .(paste(round(lm1$coefficients[2], 2), 
as.expression(unit.label))))
)

Where am I going wrong? Is it a simple syntax problem in my bquote command? Thanks for any suggestions!

解决方案

1) parse char string Create the character string desired (ensuring that it represents an expressoin that is syntactically valid in R) and then parse it. Here main_s is the character string:

fm <- lm(y ~ x)

main_s <- paste("slope:", round(coef(fm)[2], 2), "~", unit.label)
plot(0, main = parse(text = main_s))

The statement setting main_s could alternately be replaced with the following sprintf statement which is arguably more readable:

main_s <- sprintf("slope: %.2f ~ %s", coef(fm)[2], unit.label)

2) bquote The above is probably the most straight-forward way to handle this but to use bquote try this where unit.label_c is a call object and fm is as defined above:

unit.label_c <- parse(text = unit.label)[[1]]
plot(0, main = bquote(slope: .(round(coef(fm)[2], 2)) ~ .(unit.label_c)))

In either case we get this:

UPODATE Added (2). Also some improvements and corrections.

这篇关于bquote:如何包含一个保存为字符串对象的表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆