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.
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.
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())
scales::comma
is a shorthand for scales::label_comma
, etc. –
Liberalism Error: Breaks and labels are different lengths
–
Spathic library(scales)
–
Pneumothorax labels = scales::comma
instead of just labels = comma
. I guess I have some masking going on but I'm not sure what exactly. –
Lazor Did you try something like :
options(scipen=10000)
before plotting ?
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)
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 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)
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))
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))
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))
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.
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.
library(scales)
ggplot(data, aes(salary)) +
geom_histogram() +
scale_x_continuous(labels = comma)
here scale_x_continuous(labels = comma)
can solve that issue.
© 2022 - 2024 — McMap. All rights reserved.