0

Is there a way to add axis ticks discretely (i.e. just 1 at a time, not redefining the whole axis)? This would be very useful to dynamically show data without having to reconfigure the whole axis breaks every time.

I'm really hoping this can be done without grabbing the data from the grob! If not possible, I'd still appreciate a best practice on how to add specific ticks/numbers to an axis.

MSR
  • 2,731
  • 1
  • 14
  • 24
Sebastian Rivas
  • 1,700
  • 2
  • 13
  • 15
  • I may be wrong, but I think it is either *"allow ggplot2 to define the ticks"* or *"define all of the ticks yourself"*. There are tools for determining what are good ticks (e.g., `axTicks`, I suspect there is a `ggplot2::` analog to that), so it shouldn't too difficult to mimic the default ticks and then add one. – r2evans May 10 '20 at 17:01
  • maybe sth like this is what you mean : https://stackoverflow.com/q/61348415/7941188 – tjebo May 10 '20 at 18:31

1 Answers1

1

You can use the pretty function:

library(ggplot2)

extra_breaks <- c(6, 21)

breaks <- sort(c(extra_breaks, with(mtcars, pretty(range(mpg)))))

ggplot(mtcars, aes(x = cyl, y = mpg)) +
  geom_point() +
  scale_y_continuous(
    breaks = breaks,
    limits = range(breaks)
  )

Created on 2020-05-10 by the reprex package (v0.3.0)

MSR
  • 2,731
  • 1
  • 14
  • 24