Scales

Scales map values in a data space to values in an aesthetic space, whether the value is a color, shape, or size. Scales are used to change legends or axes, providing inverse mapping and enabling us to understand the data from the graphic itself. In the previous example, when we plotted the histogram, ggplot applied a default scale, in order to describe the x- and y-axes. However, we can modify that scale.

To modify scales, the following commands are used:

  • Continuous variables: scale_x_continuous (x-axis), scale_y_continuous (y-axis).
  • Discrete variables: scale_x_discrete (x-axis), scale_y_discrete (y-axis).

Some elements that are commonly changed are as follows:

  • name: The first argument gives the axis or legend title.
  • limits: The ...

Get Applied Data Visualization with R and ggplot2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.