2

I'm trying to make the legend of this plot pretty, so I need there the be an actual superscript, which is why I am using the pretty10exp() function from the sfsmisc library. It works when I use the c() function.

However, I am also trying to keep the string and the scientific notation number on the same line. The legend() is broken into two lines, which I think is due to c(). I thought I could use paste(), but for some reason the output is now incorrect.

plot(1:12)
pVal <- 4
legend("topright", legend = c("P value:", sfsmisc::pretty10exp(pVal)), cex = 1.5)

legend("topright", legend = paste("P value:", sfsmisc::pretty10exp(pVal)), cex = 1.5)

pVal being an arbitrary number represented in scientific notation. The second line results in output like this: "P value: (significand) %*% 10^-4". The first line also doesn't give me what I want. How can I fix this problem?

MrFlick
  • 195,160
  • 17
  • 277
  • 295
StarckOverflar
  • 1,577
  • 2
  • 10
  • 13
  • 1
    `c()` won't work as it will create a length 2 character vector. As for making an 'actual superscript', try using the much simpler [`plotmath`](https://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/plotmath.html), although I don't know if you think that is pretty enough. – vincentmajor Aug 11 '16 at 03:27
  • I can't use plotmath, because the number in scientific notation is saved in a variable. I tried for a while unsuccessfully. Also I don't know what you mean create a length 2 character vector? – StarckOverflar Aug 11 '16 at 03:45
  • It would help if you provided a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to test with. Make sure all variables and functions are defined. – MrFlick Aug 11 '16 at 03:51
  • Well, any example will work. I should have explained what pVal is, though. It's just an arbitrary number in scientific notation. – StarckOverflar Aug 11 '16 at 04:00
  • Here is an example: plot(1:12); pVal <- 4; and try one of the following lines after that. – StarckOverflar Aug 11 '16 at 04:01

2 Answers2

2

pretty10exp returns an expression which allows it to use the ?plotmath features for making nice looking numbers. When working with expressions, you can't just paste values in like strings. You need to manipulate them with a special set of functions. One such function is substitute. You can do

plot(1:12)
pVal <- 4
legend("topright", cex = 1.5, 
    legend = substitute("P value: "*x, list(x=sfsmisc::pretty10exp(pVal)[[1]])) )

We use substitute() to take the value contained in the expression from pretty10exp and prefix it with the label you want. (We use * to concatenate rather than paste() since plotmath allows it)

enter image description here

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • 1
    This is too complicated. Just write the whole expression with bquote which lets you use a value from a variable in it. No need for a package. – Roland Aug 11 '16 at 06:29
  • How would you use bquote? I tried it, but it doesn't work when the argument is a variable. You'd have to hard code the number in. – StarckOverflar Aug 11 '16 at 15:21
  • @Bob I suggest reading the documentation. I might write an answer later. – Roland Aug 11 '16 at 17:48
  • @Roland I still think it's easier to modify the expression rather than recode the logic in pretty10exp. I guess if you wanted to support fewer features than maybe it wouldn't be so bad but I didn't want to bother with recreating the formatting code. – MrFlick Aug 11 '16 at 18:29
0

This is what I would do:

fun <- function(text, pVal) {
  y <- floor(log10(pVal))
  x <- pVal / 10^y
  bquote(.(text)*":" ~ .(x) %.% 10 ^ .(y))
}

plot.new()
text(0.5,0.7,fun("P value", 0.4))
text(0.5, 0.3, fun("P value", signif(1/pi, 1)))

resulting plot

No package is needed.

Roland
  • 127,288
  • 10
  • 191
  • 288