Part 57 Position scales

The scales for x and y control positioning, limits, breaks, labels, titles, etc. of the axes.

  • scale_x_continuous()
  • scale_x_discrete()
  • scale_x_binned()

57.1 Arguments

  1. name()
  • The name of the scale. Used for the axis/legend title.
  • Give multiple scales the same name to combine them.
  • scale_x_continuous(name = "MPG")
  1. breaks
  • Where to break the scale (e.g., where to bin or where to place labeled tick marks)
  • To override the default computed breaks, pass a vector of values
  • (There are more advanced options as well)
  • scale_x_continuous(name = "MPG", breaks = c(0, 10, 20, 30))
  1. labels
  • Override the existing axis labels
  • Usually given as a function that transforms the text
  • e.g.,
    • Numbers and Dates
      • scales::label_percent
      • scales::label_dollar
      • scales::label_date
      • scales::label_comma
      • scales::label_pvalue
    • Text
      • stringr::str_to_upper
      • stringr::str_to_sentence
      • scales::label_parse / scales::label_math
  1. limits
  • The minimum and maximum values for the scale
  • e.g., scale_x_continuous(limits = c(40, 100))
  1. oob
  • What to do with values outside the limits
  • Defaults
    • Continuous: scales::oob_censor
    • Binned: scales::oob_squish
  • scales::oob_keep: Like zooming, don’t remove or change values.
ggplot(mtcars) + 
  aes(
    x = hp, y = mpg, 
    color = factor(cyl), 
    shape = hp > 200
  ) + 
  geom_point(size = 3) + 
  scale_x_continuous(
    limits = c(0, 200), 
    oob = scales::oob_squish
  )
  1. trans
  • A transformation function to apply to each value of the scales
  • Must be a quoted function name
  • e.g., “sqrt” or “rev” or “log10”
  • scale_x_continuous(trans = "sqrt")
  • scale_x_sqrt(), scale_x_reverse(), and scale_x_log10() are shortcuts
  1. position
  • “top”, “bottom”, “left”, or “right”
  • Can also be set in guide_axis()
  1. guide
  • A function controlling formatting of the guide
  • I usually set in guides() instead
  1. na.translate
  • TRUE or FALSE, for discrete scales, plot NA?
  • scale_x_discrete(na.translate = FALSE)
  1. na.value
  • What value to replace NA with.
  • scale_x_discrete(na.translate = TRUE, na.value = "(Missing)")