Home > Mobile >  Dynamic decimal precision in ggplot axis labels
Dynamic decimal precision in ggplot axis labels

Time:06-25

I would like to display only the minimum number of decimal places in my ggplot axis labels (e.g. 0.1 with 10 as opposed to 10.0. I was trying to get scales::label_number() to do this, but the accuracy argument is applied across all of the labels. Also I'd like to be able to add big.mark = "," if possible.

The closest enter image description here

Though if you wanted a bit more control, then you could use prettyNum - e.g.

tibble(x = -2:3, y = 10^x) %>% 
  ggplot(aes(x, y))   
  geom_point()  
  scale_y_log10(labels = ~ prettyNum(.x, big.mark = ","), breaks = 10^(-2:3))

enter image description here

  • Related