READ COMPREHENSIVE NOTE ON mapping:

Mapping Variables to Aesthetics in ggplot2

When creating plots in R using ggplot2, you often want to show how different data points relate to each other. To do this, you map variables in your data to visual elements (aesthetics) in your plot. Think of aesthetics as the visual aspects of your plot, such as the position of points, their color, size, or shape.

Two Main Ways to Use Aesthetics

  1. Mapping Variables to Aesthetics:
    • What it means: When you map a variable to an aesthetic, the visual attribute (like color, size, shape) changes according to the values of that variable. It is more of additional dimension to the data visualisation.
    • Why you do it: This helps to visually distinguish data points based on their values, making patterns and relationships easier to see.
  2. Setting a Fixed Value for an Aesthetic:
    • What it means: When you set an aesthetic to a fixed value, all data points will have the same visual attribute.
    • Why you do it: This can simplify the plot and make it easier to focus on other aspects of the data.

Mapping Variables to Aesthetics

Setting Fixed Values for Aesthetics

Comparing the Two Approaches

Mapping Variables:

  • Pros:
    • Reveals patterns and relationships in the data.
    • Allows for a richer, more informative visualization.
    • Helps to identify how different factors interact.
  • Cons:
    • Can become visually complex and harder to read if overdone.

Setting Fixed Values:

  • Pros:
    • Simplifies the plot, making it easier to see the main relationship.
    • Useful for highlighting specific trends without additional variables.
  • Cons:
    • Misses out on showing interactions and patterns related to other variables.

Summary

  • Mapping Variables to Aesthetics: Use aes() within ggplot() to link data variables to visual properties like color and size. This makes the plot dynamically reflect the data values.
  • Setting Fixed Values for Aesthetics: Set aesthetic properties directly within geom_*() functions to apply a constant value, simplifying the plot and focusing on specific relationships.

Understanding when to map variables and when to set fixed values will help you create more effective and clear visualizations tailored to your specific data analysis needs.