Drawing multiple layers with ggplot2

ANU BDSI
workshop
Data Visualisation with R Part 1

Emi Tanaka

Biological Data Science Institute

10th April 2024

Current learning objective

  • -Create basic plots using ggplot2
  • -Understand the concept of the grammar of graphics
  • Construct plots with multiple layers in ggplot2
  • -Adjust scales and guides within ggplot2

Layering plots

  • You can add more than one layer.
  • The order of layer matters.

Inheriting data and mapping

  • A layer inherits the data and mapping from the initialised ggplot object by default

Layer-specific data and aesthetic mapping

  • For each layer, aesthetic and/or data can be overwritten.

Illustrative data 🚜 Iowa farmland values by county

Layer specific aesthetic

Layer specific data

Layer specific data as a function of inherited data

Annotation layer

  • annotate() allows you to add elements to plots without a data.frame

Summary

  • You can construct plots with multiple layers in ggplot2.
  • The order of the layer matters.

  • A layer inherits the data and mapping from the initialised ggplot object by default
  • But the data and mappings for each layer can be overwritten.

Exercise time

20:00