If you don't want any trailing zeros, you can use labels = scales::label_number(drop0trailing=TRUE)
in a scale_*
function:
library(ggplot2)
iris |> ggplot(aes(x = Petal.Length, y= Petal.Width)) +
geom_point() +
scale_y_continuous(labels = scales::label_number(drop0trailing=TRUE))
Here, I used scales::label_number
(the scales
package is anyway required by ggplot2
) with the drop0trailing=TRUE
parameter, which will be passed down to base::format
which in turn will pass it down to base::prettyNum
where this functionality is finally documented:
?prettyNum
:
drop0trailing
logical, indicating if trailing zeros, i.e., "0" after the decimal
mark, should be removed; also drops "e+00" in exponential formats.
[...]
For reference, this is how the y axis labels would look by default:
iris |> ggplot(aes(x = Petal.Length, y= Petal.Width)) +
geom_point()
Created on 2024-01-25 with reprex v2.0.2