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

131,229

Solution 1

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)

Solution 2

Did you try something like :

options(scipen=10000)

before plotting ?

Solution 3

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)

Solution 4

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)

Solution 5

There is a solution that don't require scales library.

You can try:

# To deactivate scientific notation on y-axis:

    p + scale_y_continuous(labels = function(x) format(x, scientific = FALSE))

# To activate scientific notation on y-axis:

    p + scale_y_continuous(labels = function(x) format(x, scientific = TRUE))

# To deactivate scientific notation on x-axis:

    p + scale_x_continuous(labels = function(x) format(x, scientific = FALSE))

# To activate scientific notation on x-axis:

    p + scale_x_continuous(labels = function(x) format(x, scientific = TRUE))
Share:
131,229
JPD
Author by

JPD

Updated on July 12, 2022

Comments

  • JPD
    JPD almost 2 years

    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 values which in this case would be 1,10.

    Any help much appreciated.

  • JPD
    JPD over 11 years
    This worked. Thank you. Out of interest, what other 'label' options are there for axes in ggplot2 with the scales package?
  • Marta Karas
    Marta Karas almost 10 years
    Please visit also this ggplot2.org page, it was very helpful for me with a similar issue.
  • ecoe
    ecoe almost 8 years
    This works by setting a higher penalty for deciding to use scientific notation. More explanation in this answer: stackoverflow.com/a/18600721/1080804
  • cincodenada
    cincodenada over 4 years
    @Arun's answer should work fine as-is, perhaps you neglected to include the require(scales)? This imports the package that contains the comma scale. As you've discovered, you can also specify the package when referring to it instead of requiring it beforehand.
  • Ollie Perkins
    Ollie Perkins over 3 years
    This is much nicer from an aesthetic perspective!
  • tjebo
    tjebo about 3 years
    That link is outdated. Now you want to have a look at ggplot2-book.org/scale-position.html#label-functions - scales::comma is a shorthand for scales::label_comma, etc.
  • tjebo
    tjebo about 3 years
    I agree with @cincodenada. this answer does not add anything new. I will not downvote, because it's not a wrong answer. Just the same as above.
  • tjebo
    tjebo about 3 years
  • SJ9
    SJ9 about 3 years
    format_format is currently retried from package scales. you should either use label_number() or label_date() instead.
  • Joshua Eric Turcotte
    Joshua Eric Turcotte almost 3 years
    hmm; just trying this, I'm getting a new error: Error: Breaks and labels are different lengths
  • George Hayward
    George Hayward about 2 years
    This didn't work for me until I saw this comment ...which basically said that you needed to run first library(scales)
  • stevec
    stevec about 2 years
    It always seems to error for me until I use labels = scales::comma instead of just labels = comma. I guess I have some masking going on but I'm not sure what exactly.