I'm using pretty10exp()
from the sfsmisc
package to make the scientific notation look better. For example:
library(sfsmisc)
a <- 0.000392884
The output of pretty10exp()
looks like this:
> pretty10exp(a, digits.fuzz=3) #round to display a certain number of digits
expression(3.93 %*% 10^-4)
I can use this to display the pretty version of a
on a graph's title or axes as described in this post:
Force R to write scientific notations as n.nn x 10^-n with superscript
However, things get ugly again when I try to combine it with paste()
to write a sequence of character strings like this:
# 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)))
Which gives me the following graph, so I suppose paste()
is not able to handle formatted expressions of the kind that can be found in the output of pretty10exp()
:
Is there an alternative to paste()
that I could use to combine the expressions "p =" and the scientific notation of pretty10exp()
?
One solution is just to copy what pretty10exp()
does, which for a single numeric, a
, and the options you set/defaults, is essentially:
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)
The equivalent using bquote()
would be
SS <- bquote(p == .(mT) %*% 10^.(eT))