เปลี่ยนรูปแบบของตัวเลขเป็นแบบ m*10^n บนแกน ใน ggplot

มีตัวอย่างที่ code ที่นำมาใช้ได้ที่ https://groups.google.com/forum/#!topic/ggplot2/a_xhMoQyxZ4 (โดย Brian Diggs)

fancy_scientific <- function(l) {
 # turn in to character string in scientific notation
 l <- format(l, scientific = TRUE)
 l <- gsub("0e\\+00","0",l)
 # quote the part before the exponent to keep all the digits
 l <- gsub("^(.*)e", "'\\1'e", l)
 # turn the 'e+' into plotmath format
 l <- gsub("e", "%*%10^", l)
 # return this as an expression
 parse(text=l)
}

เอาไปใช้งาน

library(ggplot2) 
library(scale)
 ggplot(data=df, aes(x=x, y=y)) +
 geom_point() +
 scale_y_continuous(labels=fancy_scientific)

 

 

%d bloggers like this: