Search code examples
rscientific-notation

R solution for scientific notation formatting


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!


Solution

  • 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