177

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.

tjebo
  • 21,977
  • 7
  • 58
  • 94
JPD
  • 2,561
  • 5
  • 22
  • 26

10 Answers10

212

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
library(scales)
p + scale_x_continuous(labels = label_comma())
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
Arun
  • 116,683
  • 26
  • 284
  • 387
  • 2
    This worked. Thank you. Out of interest, what other 'label' options are there for axes in ggplot2 with the scales package? – JPD Jan 28 '13 at 14:28
  • 3
    Please visit also [this ggplot2.org page](http://docs.ggplot2.org/current/scale_continuous.html), it was very helpful for me with a similar issue. – Marta Karas Jul 30 '14 at 10:42
  • 3
    That link is outdated. Now you want to have a look at https://ggplot2-book.org/scale-position.html#label-functions - `scales::comma` is a shorthand for `scales::label_comma`, etc. – tjebo Feb 07 '21 at 14:13
  • 2
    hmm; just trying this, I'm getting a new error: `Error: Breaks and labels are different lengths` – Joshua Eric Turcotte Jul 13 '21 at 12:24
  • 1
    This didn't work for me until I saw [this comment](https://stackoverflow.com/a/11995652/11736959) ...which basically said that you needed to run first `library(scales)` – George Hayward Mar 20 '22 at 18:27
  • 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. – stevec Mar 28 '22 at 14:33
  • If I use dual y-axis, this solution seems will not work out. – ah bon Dec 29 '22 at 02:44
  • I found that the scales::comma solution worked but removed my log10 scaling in the process – Markm0705 Feb 03 '23 at 06:12
95

Did you try something like :

options(scipen=10000)

before plotting ?

juba
  • 47,631
  • 14
  • 113
  • 118
  • 7
    This works by setting a higher *penalty* for deciding to use scientific notation. More explanation in this answer: http://stackoverflow.com/a/18600721/1080804 – ecoe Jul 04 '16 at 14:09
61

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)
alessandra
  • 135
  • 1
  • 5
Derek Corcoran
  • 3,930
  • 2
  • 25
  • 54
  • 4
    @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. – cincodenada Sep 17 '19 at 00:45
26

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)
user2739472
  • 1,401
  • 17
  • 15
  • 4
    format_format is currently retried from package scales. you should either use label_number() or label_date() instead. – SJ9 Apr 11 '21 at 18:14
21

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))
Willian Adamczyk
  • 1,691
  • 1
  • 9
  • 7
9

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))
mdeleeuw
  • 91
  • 1
  • 1
5

If you additionally want to have commas as a thousand separator, you can use the following:

p + scale_x_continuous(labels=function(x) format(x, big.mark = ",", scientific = FALSE))
PeterD
  • 1,331
  • 12
  • 22
2
p + scale_x_continuous(labels = scales::number_format(accuracy = 1))

the accuracy = 1 is for whole numbers, you could also use accuracy = 0.1 if you wanted one decimal place, accuracy = 0.01 for two decimal places, etc.

similar to this answer

tassones
  • 891
  • 5
  • 18
1

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.

-1
library(scales)

ggplot(data, aes(salary)) +
  geom_histogram() +
  scale_x_continuous(labels = comma)

here scale_x_continuous(labels = comma) can solve that issue.

Sabareesh
  • 9
  • 2
  • While this code snippet may be the solution, including a detailed explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Shawn Hemelstrand Mar 26 '23 at 19:49