
If you need to specify exact breaks it is better to do so manually.īase <- ggplot ( toy, aes ( big, const ) ) + geom_point ( ) + labs (x = NULL, y = NULL ) + scale_y_continuous (breaks = NULL ) base base + scale_x_continuous ( breaks = c ( 2000, 4000 ), labels = c ( "2k", "4k" ) ) Note that breaks_extended() treats n as a suggestion rather than a strict constraint.

I can alter the desired number of breaks by setting n = 2, as illustrated in the third plot. The breaks_extended() function is the standard method used in ggplot2, and accordingly the first two plots below are the same. scales::breaks_width() creates equally spaced breaks.scales::breaks_pretty() creates “pretty” breaks for date/times.scales::breaks_log() creates breaks appropriate for log axes.scales::breaks_extended() creates automatic breaks for numeric axes.31 It provides several tools that are useful for this purpose:

You can write your own break function, but in many cases there is no need, thanks to the scales package. This function should have one argument that specifies the limits of the scale (a numeric vector of length two), and it should return a numeric vector of breaks. It is also possible to pass a function to breaks. To learn more about how “out of bounds” values are handled for continuous and binned scales, see Section 15.4.īase + scale_x_continuous (breaks = c ( 1000, 2000, 4000 ) ) base + scale_x_continuous (breaks = c ( 1000, 1500, 2000, 4000 ) ) You can learn more about coordinate systems in Section 16.1. Unfortunately it would be very hard to change this default without breaking a lot of existing code. In hindsight, I regret this design choice as it is a common source of confusion for users. Because these “out of bounds” values are no longer available, the end result is that the sample median is shifted downward, which is almost never desirable behaviour. When modifying the scale limits, all observations with highway mileage greater than 35 are converted to NA before the stat (in this case the boxplot) is computed. In contrast, in the plot on the right one of the boxplots has changed. Some of the outlier points are not shown due to the restriction of the range, but the boxplots themselves remain identical. The only difference between the left and middle plots is that the latter is zoomed in.


Because dates and times are a little more complicated than a standard continuous variable, ggplot2 provides special scales to help you control the major and minor breaks (Sections 10.2.1 and 10.2.2) and the labels (Section 10.2.3) for date/time data.
