How can I format axis labels with exponents with ggplot2 and scales?

后端 未结 5 1098
醉梦人生
醉梦人生 2020-11-28 08:06

With the new version ggplot2 and scales, I can\'t figure out how to get axis label in scientific notation. For example:

x <- 1:4
y <- c(0, 0.0001, 0.00         


        
5条回答
  •  暖寄归人
    2020-11-28 08:54

    As per the comments on the accepted solution, OP is looking to format exponents as exponents. This can be done with the trans_format and trans_breaks functions in the scales package:

        library(ggplot2)
        library(scales)
    
        x <- 1:4
        y <- c(0, 0.0001, 0.0002, 0.0003)
        dd <- data.frame(x, y)
    
        ggplot(dd, aes(x, y)) + geom_point() +
        scale_y_log10("y",
            breaks = trans_breaks("log10", function(x) 10^x),
            labels = trans_format("log10", math_format(10^.x)))
    

提交回复
热议问题