How does the breaks argument work in scale_*_continuous?

Hi there,

I just wanted to expand on what was said earlier. In general, there aren't a ton of situations where you would want your breaks to go beyond the limits of the data, but if you do, you can use the expand function within the scale_x_continuous function to change where the graph ends, and if you make it go further than the default for your data, you can then add breaks that are farther than your data.
For example:

library(tidyverse)

tibble(x = 1:5, y = c('5', '4', '3', '2', '1'))  |> 
  ggplot(aes(x, y)) + 
  geom_point()

If the above is the basic dataset you're working with, you can expand the edges of the x axis and add breaks in that new region of the x-axis.


tibble(x = 1:5, y = c('5', '4', '3', '2', '1'))  |> 
  ggplot(aes(x, y)) + 
  geom_point() + 
  scale_x_continuous(
    breaks = c(1, 3, 7), # an example of manipulating the breaks how you choose
    expand = expansion(mult = c(1.04, 1.04))
  ) 

Created on 2023-10-26 with reprex v2.0.2

I'm not sure if this is what you're looking for, but I hope it helps.

2 Likes