How to accurately display the SI prefix for numbers in the scale along the Y axis of a graph made with ggplot2 in R?

I have the following graph generated using this code

plt <- ggplot(d2, aes_string(x=names(same_df)[1],y= "value")) + geom_point(aes(color = variable), size = 1)+ theme_bw()+ theme(legend.text=element_text(size=14), legend.title=element_text(size=14))+ theme(axis.text=element_text(size=20)) + theme(axis.title=element_text(size=20,face="bold")) + scale_color_discrete(name = "title", labels = c("1", "2", "3", "4","5","6","7","8","9")) + labs(x = "x", y = "y")+ guides(colour = guide_legend(override.aes = list(size=4),ncol=2,title.hjust=0.5))+theme(plot.margin=unit(c(0,0,0,0),"mm")) 

enter image description here

However, I need the SI prefix notation for numbers along the y axis to get the following steps:

 library("sos") 

To use findFn package in sitools

 findFn("{SI prefix}") 

Then I use f2si in the methods to convert the number of floating point numbers to a number with the SI prefix

 plt2 <- plt + scale_y_continuous(labels=f2si) 

The resulting plot is as follows:

enter image description here

While f2si exactly changed the y axis for -1e ^ -0.8 to -10 n, it does not accurately display the value for 0 and 1e ^ -0.8, which will be 0 and 10 n, respectively. Can someone please suggest what needs to be fixed here so that the numbers are displayed as they should be throughout.

Thanks.

+6
source share
1 answer

I could not reproduce your behavior. See this:

 df <- data.frame(x=runif(100), y=(runif(100)-1/2)/1e8) p <- ggplot(df, aes(x, y)) + geom_point() p + scale_y_continuous(labels=f2si) 

enter image description here

I also found another similar function if you don't like the label "0 n":

  format_si <- function(...) { # Based on code by Ben Tupper # https://stat.ethz.ch/pipermail/r-help/2012-January/299804.html function(x) { limits <- c(1e-24, 1e-21, 1e-18, 1e-15, 1e-12, 1e-9, 1e-6, 1e-3, 1e0, 1e3, 1e6, 1e9, 1e12, 1e15, 1e18, 1e21, 1e24) prefix <- c("y", "z", "a", "f", "p", "n", "Β΅", "m", " ", "k", "M", "G", "T", "P", "E", "Z", "Y") # Vector with array indices according to position in intervals i <- findInterval(abs(x), limits) # Set prefix to " " for very small values < 1e-24 i <- ifelse(i==0, which(limits == 1e0), i) paste(format(round(x/limits[i], 1), trim=TRUE, scientific=FALSE, ...), prefix[i]) } } p + scale_y_continuous(labels=format_si()) 

enter image description here

+3
source

All Articles