Question

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():

enter image description here

Is there an alternative to paste() that I could use to combine the expressions "p =" and the scientific notation of pretty10exp()?

Was it helpful?

Solution

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

OTHER TIPS

Definitely not a precise answer, but I just play around with things for a while. bquote is nice once you get a feel for it.

> call("rep", 10, 7)
## rep(10, 7)

> bquote(.(call("rep", 10, 7)) * q^5)
## rep(10, 7) * q^5

> sprintf("paste('%smm'^'%s')", 5, 5)
## [1] "paste('5mm'^'5')"

And my personal favorite, which will of course return TRUE... just as soon as I someone writes is.awesome.

> bquote( f <- function(x) { is.awesome(R) })
## f <- function(x) {
##     is.awesome(R)
## }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top