2

I am looking for a solution to format numbers in R under scientific notation, using the standard format a × 10 b. This is required in several peer-reviewed scientific journals, and modifying graphs by hand may become tedious.

Below, an example of how the R standard "E notation" looks like, and how I would like it to look like:

var<-0.00000000000000000000000000000031267781238
plot(1,main=var)

R scientific notation

Thanks for any suggestion you will have!

Thrawn
  • 87
  • 6

2 Answers2

4

A solution exists in the function scinot, from the recent version of the R package corto, available on CRAN. The function generates an expression out of a single value (or a vector of values), which can then be printed with the correct superscript.

Usage:

library(corto)    
scinot(0.00000543) # expression("5.43" ~ x ~ 10^"-6")

A "graphical" demonstration, taken from the function examples:

numbers<-c(3.456e-12,0.00901,5670000,-3.16e18,0.000004522,rnorm(5,sd=0.0000001))
plot(0,xlim=c(0,10),ylim=c(0,10),type="n")
text(c(2,6),c(10,10),labels=c("Before","After"),font=2)
for(i in 10:1){
    text(c(2,6),c(i-1,i-1),labels=c(numbers[i],scinot(numbers)[i]))
}

enter image description here

Federico Giorgi
  • 10,495
  • 9
  • 42
  • 56
1

You can use this function (with times and minus symbols), inspired by this answer. You can use "~ x ~ 10^" if you want an x.

scientific_10 <- function(x, ...) {
  parse(text = gsub("e", "%*%10^", scales::label_scientific(...)(x)))
}

plot(1, main = scientific_10(var, digits = 5))

enter image description here

Maël
  • 45,206
  • 3
  • 29
  • 67
  • 1
    I liked your previous version (with the times symbol) better. I know @Thrawn has an "x" in their example but typographically it is not correct. Your solution is also better in that it prints minus signs as minus signs, not as dashes. – Robert Hacken Feb 23 '23 at 09:59