14.4 Scale breaks

In the same way that the name argument to a scale function governs axis titles and legend titles, the breaks argument controls which values appear as tick marks on axes and as keys on legends.

toy <- data.frame(
  const = 1, 
  up = 1:4,
  txt = letters[1:4], 
  big = (1:4)*1000,
  log = c(2, 5, 10, 2000)
)
toy
#>   const up txt  big  log
#> 1     1  1   a 1000    2
#> 2     1  2   b 2000    5
#> 3     1  3   c 3000   10
#> 4     1  4   d 4000 2000
axs <- ggplot(toy, aes(big, const)) + 
  geom_point() + 
  labs(x = NULL, y = NULL)

axs
axs + scale_x_continuous(breaks = c(2000, 4000))

The examples below illustrate the same ideas applied to legends:

leg <- ggplot(toy, aes(up, up, fill = big)) + 
  geom_tile() + 
  labs(x = NULL, y = NULL) 

leg 
leg + scale_fill_continuous(breaks = c(2000, 4000))

Another way to modify the behaviour of axes and legends is with the guide argument of the relevant scale function or—perhaps more conveniently—the guides() helper function. The guides() helper works in a similar way to the labs() helper function described in Section 8.4. Both take the name of different aesthetics (e.g., color, x, fill) as arguments and allow you to specify your own value. Where labs() provides a shorthand way to specify the name argument to one or more scales, the guides() function allows you to specify guide arguments to one or more scales. In the same way that labs(colour = "a colour scale name") specifies the name associated with the colour scale, a command such as guides(colour = guide_coloursteps()) can be used to specify its associated guide:

base <- ggplot(mpg, aes(displ, hwy, colour = cyl)) + geom_point()

base 
base + scale_colour_continuous(guide = guide_coloursteps())
base + guides(colour = guide_coloursteps())

Scale guides are more complex than scale names: where the name argument (and labs() ) takes text as input, the guide argument (and guides()) require a guide object created by a guide function such as guide_colourbar() and guide_legend(). These arguments to these functions offer additional fine control over the guide.

The table below summarises the default guide functions associated with different scale types:

Scale type Default guide type
continuous scales for colour/fill aesthetics colourbar
binned scales for colour/fill aesthetics coloursteps
position scales (continuous, binned and discrete) axis
discrete scales (except position scales) legend
binned scales (except position/colour/fill scales) bins

The guide functions have numerous examples in the documentation that illustrate all of their arguments. Many of the arguments to the guide function are equivalent to theme settings (Chapter 17) like text colour, size, font etc, but only apply to a single guide. Here I’ll focus on the non-theming arguments.