我正在使用pretty10exp()包中的sfsmisc来使科学计数法看起来更好。例如:

library(sfsmisc)
a <- 0.000392884
pretty10exp()的输出如下所示:
> pretty10exp(a, digits.fuzz=3) #round to display a certain number of digits
expression(3.93 %*% 10^-4)

我可以使用它来在图形的标题或轴上显示a的漂亮版本,如这篇文章中所述:
Force R to write scientific notations as n.nn x 10^-n with superscript

但是,当我尝试将其与paste()组合以编写如下所示的字符串序列时,情况又变得很难看:
# some data
x <- seq(1, 100000, len = 10)
y <- seq(1e-5, 1e-4, len = 10)

# default plot
plot(x, y)
legend("topleft", bty="n",legend=paste("p =", pretty10exp(a, digits.fuzz=3)))

这给了我下面的图,所以我认为paste()无法处理可以在pretty10exp()输出中找到的那种格式化表达式:

我可以使用paste()的替代方法来组合表达式“p =”和pretty10exp()的科学记法吗?

最佳答案

一种解决方案是仅复制pretty10exp()的功能,对于单个数字a以及您设置/默认的选项,其本质上是:

a <- 0.00039288
digits.fuzz <- 3
eT <- floor(log10(abs(a)) + 10^-digits.fuzz)
mT <- signif(a/10^eT, digits.fuzz)
SS <- substitute(p == A %*% 10^E, list(A = mT, E = eT))
plot(1:10)
legend("topleft", bty = "n", legend = SS)

使用bquote()的等效项是
SS <- bquote(p == .(mT) %*% 10^.(eT))

关于r - paste()的替代方案,以连接图形上的格式化文本表达式?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22853126/

10-09 18:41