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
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")
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))
labels
- Override the existing axis labels
- Usually given as a function that transforms the text
- e.g.,
- Numbers and Dates
scales::label_percentscales::label_dollarscales::label_datescales::label_commascales::label_pvalue
- Text
stringr::str_to_upperstringr::str_to_sentencescales::label_parse/scales::label_math
- Numbers and Dates
limits
- The minimum and maximum values for the scale
- e.g.,
scale_x_continuous(limits = c(40, 100))
oob
- What to do with values outside the
limits - Defaults
- Continuous:
scales::oob_censor - Binned:
scales::oob_squish
- Continuous:
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
)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(), andscale_x_log10()are shortcuts
position
- “top”, “bottom”, “left”, or “right”
- Can also be set in
guide_axis()
guide
- A function controlling formatting of the guide
- I usually set in
guides()instead
na.translate
- TRUE or FALSE, for discrete scales, plot NA?
scale_x_discrete(na.translate = FALSE)
na.value
- What value to replace
NAwith. scale_x_discrete(na.translate = TRUE, na.value = "(Missing)")