I'm confused about how the breaks argument works in scale_x_continuous.

The helps says it takes a function on the limits.

Is this because the default limits are the range of the data + the default expanding? I understood that the limits were not inclusive of the expand??

```
library(ggplot2)
diamonds |>
ggplot() +
geom_point(aes(x = carat, y = price)) +
scale_x_continuous(breaks = \(x) pretty(x, n = 7))
```

```
diamonds |>
ggplot() +
geom_point(aes(x = carat, y = price)) +
scale_x_continuous(breaks = pretty(range(diamonds$carat), n = 7))
```

^{Created on 2023-10-20 with reprex v2.0.2}

All the arguments to the `breaks`

parameter wind up in the same place—a numeric vector of values along the `x`

-axis at which a vertical grid line will be drawn. These range from `NULL`

through the default (`breaks = waiver())`

, to any arbitrary function that returns a vector that is within the inclusive range of the data being plotted along the `x`

-asis. `diamonds$caret`

has range [0.2,5.01], so an attempt to use

```
scale_continuous(breaks = c(0,6)
```

would not extend the range.

Thanks!

I wondered if the breaks function is applied the sum of the limits + expand?

I didn’t see a way to extend the breaks beyond the range of the data.

1 Like

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

Here are some more examples of how `scale_x_continuous()`

and `scale_y_continuous()`

works:

Here is a plot before scaling any axes:

```
tibble(x = 1:10, y = 1:10) %>%
ggplot(aes(x, y)) +
geom_point()
```

^{Created on 2023-10-26 by the reprex package (v2.0.1)}

The `breaks = `

argument in `scale_*_continuous()`

takes in a numerical vector which will be the values displayed on the specified axis.

Here is the same plot but with the breaks on the x-axis modified:

```
tibble(x = 1:10, y = 1:10) %>%
ggplot(aes(x, y)) +
geom_point() +
scale_x_continuous(breaks = c(2, 4, 6, 8, 10))
```

^{Created on 2023-10-26 by the reprex package (v2.0.1)}

Again, the same plot but with the breaks on the y-axis modified this time:

```
tibble(x = 1:10, y = 1:10) %>%
ggplot(aes(x, y)) +
geom_point() +
scale_y_continuous(breaks = c(2, 4, 6, 8, 10))
```

^{Created on 2023-10-26 by the reprex package (v2.0.1)}

1 Like

system
Closed
November 16, 2023, 9:18pm
7
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.