1. Learn
  2. /
  3. Courses
  4. /
  5. Intermediate Data Visualization with ggplot2

Connected

Exercise

Expand and clip

The coord_*() layer functions offer two useful arguments that work well together: expand and clip.

  • expand sets a buffer margin around the plot, so data and axes don't overlap. Setting expand to 0 draws the axes to the limits of the data.
  • clip decides whether plot elements that would lie outside the plot panel are displayed or ignored ("clipped").

When done properly this can make a great visual effect! We'll use theme_classic() and modify the axis lines in this example.

Instructions 1/2

undefined XP
  • 1
    • Add Cartesian coordinates with zero expansion, to remove all buffer margins on both the x and y axes.
  • 2

    Setting expand to 0 caused points at the edge of the plot panel to be cut off.

    • Set the clip argument to "off" to prevent this.
    • Remove the axis lines by setting the axis.line argument to element_blank() in the theme() layer function.