Force R to stop plotting abbreviated axis labels - e.g. 1e+00 in ggplot2
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))
JPD
Updated on July 12, 2022Comments
-
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 be1,10
.Any help much appreciated.
-
JPD over 11 yearsThis worked. Thank you. Out of interest, what other 'label' options are there for axes in ggplot2 with the scales package?
-
Marta Karas almost 10 yearsPlease visit also this ggplot2.org page, it was very helpful for me with a similar issue.
-
ecoe almost 8 yearsThis works by setting a higher penalty for deciding to use scientific notation. More explanation in this answer: stackoverflow.com/a/18600721/1080804
-
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 thecomma
scale. As you've discovered, you can also specify the package when referring to it instead of requiring it beforehand. -
Ollie Perkins over 3 yearsThis is much nicer from an aesthetic perspective!
-
tjebo about 3 yearsThat link is outdated. Now you want to have a look at ggplot2-book.org/scale-position.html#label-functions -
scales::comma
is a shorthand forscales::label_comma
, etc. -
tjebo about 3 yearsI 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 about 3 yearssame as this answer stackoverflow.com/a/14564026/7941188
-
SJ9 about 3 yearsformat_format is currently retried from package scales. you should either use label_number() or label_date() instead.
-
Joshua Eric Turcotte almost 3 yearshmm; just trying this, I'm getting a new error:
Error: Breaks and labels are different lengths
-
George Hayward about 2 yearsThis didn't work for me until I saw this comment ...which basically said that you needed to run first
library(scales)
-
stevec about 2 yearsIt always seems to error for me until I use
labels = scales::comma
instead of justlabels = comma
. I guess I have some masking going on but I'm not sure what exactly.