在r绘图文字中指定小数位? [英] Specify decimal places in r plot text?

查看:148
本文介绍了在r绘图文字中指定小数位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图格式化在基本图形系统中创建的回归曲线的标签.基本上,此标签从变量中提取斜率,截距和r平方值.一个例子如下:

I tried to format the label of my regression curve created in the base graphics system. Basically, this label extracts the slope, the intercept and the r-square values from variables. An example is as below:

plot(rnorm(10), type = "n", xlim = c(0, 100), ylim = c(0, 100))
text(x = 0, y = 100, adj = c(0, NA), bquote(paste(y == .(a) * x + .(b), ", R"^2 == .(r2))), cex = 0.7)

但是,这生成了一个看起来不太聪明的标签:

However, this generated a label that did not look very smart:

y = 1.159019x+-1.537708, R<sup>2</sup>=0.7924927

我想将数字四舍五入到小数点后第二位,即

What I would like is to round the figures to the second decimal place, i.e.,

y = 1.16x-1.54, R<sup>2</sup>=0.79

我在text()bquote()的帮助文档中都进行了查找,但是找不到很多有用的信息.

I have looked it up in both help documents for text() and bquote() but did not find much useful information.

我还尝试用参数nsmall = 2.(a).(b).(r2)包装在format()中,但是没有用.

I have also tried to wrap the .(a), .(b) and .(r2) in format() with the parameter nsmall = 2, but it did not work.

有人可以帮我这个忙吗?提前非常感谢!

Could someone kindly help me with this? Many thanks in advance!

ps.我认为我的问题中有一个隐藏的任务.在上面的示例中,b为负.我知道我可以在表达式中省略"+"运算符,而仅使用b的负号加入我的方程式.但是,如果我事先不知道b的符号怎么办?有没有一种巧妙的方式来形成标签,而无需使用if()进行检查,然后编写两个稍有不同的text()版本?再次感谢!

ps. I think there is a hidden quest in my question. In the above example, b is negative. I know I could probably leave out the "+" operator in my expression to just use the negative sign of b to join my equation. However, what if I do not know the sign of b in advance? Is there a clever way of forming the label without checking this with an if() and then write two very slightly different versions of text()? Thanks again!

推荐答案

要指定位数,请使用round(a, digits=2).但是,您也可以使用sprintf来处理位数和等式中的+或-: %+ 3.2f,其中%在等式中强制使用+或-,而3.2f控制位数,因此这可以解决您的两个问题.我在sprintf 这里:"B2是UTF-8字符= ^ 2的十六进制代码,而\ U是将调用该字符的控制序列."

For specifying the number of digits, use round(a, digits=2). However, you can also use sprintf to deal with both the number of digits and the + or - in your equation: e.q. %+3.2f, where % forces the + or - sign in the equation and 3.2f controls the number of digits, so this solves both your issues. I found a solution for the superscript problem in sprintf here: "B2 is the hex code for UTF-8 character = ^2 and \U is a control sequence that will call that character."

# data and regression
set.seed(1)
y = 1:10+rnorm(10)
x = 1:10
fit = lm(y~x)
b = coef(fit)[1]
a = coef(fit)[2]
r2 = summary(fit)$r.squared

# plot data and regression
plot(x, y)
abline(fit, col=2)

# add text to plot with legend() for convenient placement
legend('topleft', title='option 1', legend=sprintf("y = %3.2fx %+3.2f, R\UB2 = %3.2f", a, b, r2), bty='n', cex=0.7)

# if you prefer a pretty space between plus/minus and b:
if( b<0) {my_sign = ' - '; b = -b} else { my_sign= ' + '}
legend('bottomright', title='option 2', legend=sprintf("y = %3.2f x %s %3.2f, R\UB2 = %3.2f", a, my_sign, b, r2), bty='n', cex=0.7)

这篇关于在r绘图文字中指定小数位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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