Position Adjustments & Coordinate Systems in ggplot2

Ha Khanh Nguyen

Position Adjustments

  • What happens if you map the fill aesthetic to another variable, like clarity: the bars are automatically stacked. Each colored rectangle represents a combination of cut and clarity.

  • The stacking is performed automatically by the position adjustment specified by the position argument.
  • For bar charts, position takes the following values: "stack", "identity","fill" or "dodge".
    • The default value for position in geom_bar() is "stack".

“identity”

  • position = "identity" will place each object exactly where it falls in the context of the graph.
    • The identity position adjustment is more useful for 2d geoms, like points, where it is the default.

  • To see that overlapping we either need to make the bars slightly transparent by setting alpha to a small value, or completely transparent by setting fill = NA.

“fill”

  • position = "fill" works like stacking, but makes each set of stacked bars the same height. This makes it easier to compare proportions across groups.

“dodge”

  • position = "dodge" places overlapping objects directly beside one another. This makes it easier to compare individual values.

  • Notes: To learn more about a position adjustment, look up the help page associated with each adjustment: ?position_dodge, ?position_fill, ?position_identity, ?position_jitter, and ?position_stack.

“jitter” in geom_point()

  • In this plot, there are only 126 points, even though there are 234 observations in the dataset.
  • The values of hwy and displ are rounded so the points appear on a grid and many points overlap each other. This problem is known as overplotting.

  • By setting position = “jitter”, R adds a small amount of random noise to each point. This spreads the points out because no two points are likely to receive the same amount of random noise.

  • ggplot2 comes with a shorthand for geom_point(position = "jitter"): geom_jitter().

Coordinate Systems

  • The default coordinate system is the Cartesian coordinate system where the x and y positions act independently to determine the location of each point.

coord_quickmap()

  • coord_quickmap() sets the aspect ratio correctly for maps. This is very important if you’re plotting spatial data with ggplot2.
    • To run the following code, you first need to install the maps package.

References