Changing axis scale and decimal points in R

Viewed 61

I am trying to change both the yaxis scale and the amount of decimal places. I am using ylim() (to change y scale) and scale_y_continuous(labels = scales::number_format(accuracy = 0.01)) (from scale package to change the decimal points) but they wont work together. I am using ggplot to plot my data.

1 Answers

If you use limits in scale_y_continuous, it will work.

Also you may want to use label_number instead of number_format, because, number_format is superseded by label_number as per the documentation,

These functions are kept for backward compatibility; you should switch to label_number()/label_comma() for new code.

library(ggplot2)

ggplot(mtcars, aes(x = mpg, y = drat)) +
  geom_point() +
  scale_y_continuous(
    labels = scales::label_number(accuracy = 0.1), 
    limits = c(3, 4)
  )

enter image description here

(Using mtcars built-in data for demo)

Related