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.

2
  • 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. Commented May 10, 2020 at 17:01
  • maybe sth like this is what you mean : stackoverflow.com/q/61348415/7941188 Commented May 10, 2020 at 18:31

1 Answer 1

2

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)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.