ggplot2 Quick Reference: scale

A scale determines how an attribute of the data is mapped into an aesthetic property of a geom (e.g., the geom's position along the x axis, or a geom's fill color in a color space).

ggplot2 essentially supports the following kinds of aesthetic properties:

  • x - x position (some geoms also have other aesthetics representing x positions, such as "xend" or "xmin")
  • y - y position (some geoms also have other aesthetics representing y positions, such as "yend" or "ymin")
  • size - size of a geom
  • shape - shape of a geom
  • linetype - type of a geom's outline (e.g., dashed, dotted)
  • colour - color of a geom's outline
  • fill - color of a geom's fill
  • alpha - transparency of a geom (0=transparent ... 1=opaque)

For most (but not all) of the above kinds of aesthetic properties ggplot2 provides two different types of scales: continuous and discrete. If a data attribute is represented as an R factor (which has a known number of discrete levels), ggplot2 automatically uses a discrete scale to map that attribute to an aesthetic property, otherwise it uses a continuous scale.

The following table shows functions that ggplot2 provides to define each of the above kinds of scales. The default continuous and discrete scales for each aesthetic property are shown in bold.

x (or y) position size shape linetype colour (or fill) alpha
continuous
scale_x_continuous
scale_x_date
scale_x_datetime
scale_size_continuous
scale_area
scale_colour_gradient
scale_colour_gradient2
scale_colour_gradientn
scale_alpha_continuous
discrete
scale_x_discrete scale_size_discrete
scale_size_identity
scale_size_manual
scale_shape_discrete
scale_shape_identity
scale_shape_manual
scale_linetype_discrete
scale_linetype_identity
scale_linetype_manual
scale_colour_hue
scale_colour_brewer
scale_colour_grey
scale_colour_identity
scale_colour_manual

Guides

In a plot, scales are visually represented as "guides". For the x and y position scales, those guides are the x and y axes. For scales of all other aesthetic properties, the guides are the legends. The guides help the reader to interpret the meaning of the aesthetic properties (they help to map back from the aesthetic property to the underlying data).

Guides are important for understanding a plot. However, in some rare situations it may be necessary to turn guides off. You can turn off a legend with the legend=F argument of a scale_...(...) function. This will make the legend's space available for the panel(s) of the plot. To turn off an axis, you can use theme options to make the axis, its name, labels, and breaks (tick marks) invisible (colour=NA).