Force R to stop plotting abbreviated axis labels - e.g. 1e+00 in ggplot2

前端 未结 7 1337
遥遥无期
遥遥无期 2020-11-28 04:09

In ggplot2 how can I stop axis labels being abbreviated - e.g. 1e+00, 1e+01 along the x axis once plotted? Ideally, I want to force R to display the actual valu

相关标签:
7条回答
  • 2020-11-28 04:28

    Extending the original question to comprise fractions as well, i.e. 1, 0.1, 0.01, 0.001 etc. and avoiding trailing zeros

    p + scale_x_continuous(labels = function(x) sprintf("%g", x))
    
    0 讨论(0)
  • 2020-11-28 04:42

    As a more general solution, you can use scales::format_format to remove the scientific notation. This also gives you lots of control around how exactly you want your labels to be displayed, as opposed to scales::comma which only does comma separations of the orders of magnitude.

    For example:

    require(ggplot2)
    require(scales)
    df <- data.frame(x=seq(1, 1e9, length.out=100), y=sample(100))
    
    # Here we define spaces as the big separator
    point <- format_format(big.mark = " ", decimal.mark = ",", scientific = FALSE)
    
    # Plot it
    p  <- ggplot(data = df, aes(x=x, y=y)) + geom_line() + geom_point()
    p + scale_x_continuous(labels = point)
    
    0 讨论(0)
  • 2020-11-28 04:44

    Just an update to what @Arun made, since I tried it today and it didn't work because it was actualized to

    + scale_x_continuous(labels = scales::comma)
    
    0 讨论(0)
  • 2020-11-28 04:47

    Did you try something like :

    options(scipen=10000)
    

    before plotting ?

    0 讨论(0)
  • 2020-11-28 04:49

    I think you are looking for this:

    require(ggplot2)
    df <- data.frame(x=seq(1, 1e9, length.out=100), y=sample(100))
    # displays x-axis in scientific notation
    p  <- ggplot(data = df, aes(x=x, y=y)) + geom_line() + geom_point()
    p
    
    # displays as you require
    require(scales)
    p + scale_x_continuous(labels = comma)
    
    0 讨论(0)
  • 2020-11-28 04:49

    Isn't the simplest general solution to set the penalty that R uses for scientific notation higher?

    i.e set scipen() to a number that you are comfortable with.

    e.g. If your axis maximum on charts is likely to be 100 000, setting scipen(200000) will ensure that R (and ggplot) will use standard notation for all numbers below 200000 and there will be no requirement to add any lines to the ggplot function.

    0 讨论(0)
提交回复
热议问题