Remove trailing zeros in ggplot axis

Viewed 1427

there must surely be another way of removing trailing zeros in y axis tick labels than specifiying each label indivdiually with labels=c("0",...).

I have a number of graphs plotted in a grid and there are all trailing zeros in the y axis, in particular for the zero value (see image).

enter image description here If I have to set all labels in each graph manually that would be very cumbersome.

1 Answers

Try adding this to your ggplot:

+ scale_y_continuous(labels = function(x) ifelse(x == 0, "0", x))

Example data:

data.frame(x = 1:6, 
           y = seq(0, 0.05, 0.01)) %>% 
  ggplot(aes(x, y)) + 
  geom_point() + 
  scale_y_continuous(labels = function(x) ifelse(x == 0, "0", x))

Result:

enter image description here

Related