Force R to stop plotting abbreviated axis labels (scientific notation) - e.g. 1e+00
Asked Answered
G

10

181

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.

Galvanize answered 28/1, 2013 at 14:17 Comment(0)
J
218

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())
Jeavons answered 28/1, 2013 at 14:23 Comment(8)
This worked. Thank you. Out of interest, what other 'label' options are there for axes in ggplot2 with the scales package?Galvanize
Please visit also this ggplot2.org page, it was very helpful for me with a similar issue.Officialese
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.Liberalism
hmm; just trying this, I'm getting a new error: Error: Breaks and labels are different lengthsSpathic
This didn't work for me until I saw this comment ...which basically said that you needed to run first library(scales)Pneumothorax
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.Lazor
If I use dual y-axis, this solution seems will not work out.Captain
I found that the scales::comma solution worked but removed my log10 scaling in the processTannin
M
95

Did you try something like :

options(scipen=10000)

before plotting ?

Mouthpiece answered 28/1, 2013 at 14:19 Comment(1)
This works by setting a higher penalty for deciding to use scientific notation. More explanation in this answer: https://mcmap.net/q/137780/-forcing-a-1e3-instead-of-1000-format-in-ggplot-rResoluble
O
65

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)
Opt answered 16/3, 2017 at 16:59 Comment(1)
@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.Checkup
E
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)
Everything answered 28/11, 2017 at 17:34 Comment(1)
format_format is currently retried from package scales. you should either use label_number() or label_date() instead.Creeper
R
24

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))
Reiko answered 16/11, 2019 at 18:7 Comment(0)
Z
10

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))
Zipangu answered 19/1, 2020 at 13:45 Comment(2)
This is much nicer from an aesthetic perspective!Proffer
This also worked without upsetting log10 scale and agree it has more aesthetic appearlTannin
S
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))
Saccular answered 11/8, 2022 at 10:54 Comment(1)
This worked without upsetting my log10 scaleTannin
B
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

Briannebriano answered 5/7, 2022 at 1:47 Comment(0)
A
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.

Affirm answered 21/3, 2020 at 10:4 Comment(1)
K
-1
library(scales)

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

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

Kugler answered 26/3, 2023 at 4:43 Comment(1)
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.Iden

© 2022 - 2024 — McMap. All rights reserved.