r - paste( ) 的替代方法用于在图形上连接格式化的文本表达式?

标签 r graph output scientific-notation

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

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() 的输出中找到的那种格式化表达式>:

enter image description here

是否有 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/

相关文章:

powershell - 具有自定义 header 的Powershell For Loop

r - 如何通过fitdistrplus包选择分布参数?

r - 为日期添加一个月

R:在继承 data.frame 的 S4 对象上应用 terms.formula

python - networkx是否支持按标签进行dfs遍历

javascript - 如何将 json 数据动态链接到 chart.js

Java 大数数据类型

perl - foreach循环中的打印函数: my scalar and text does not print

r - 比较两列以匹配值并并排对齐

c++ - Qt QGraphicsView 和 QGraphicSscene 鼠标位置