Lesson 2
Adding Colors and Aesthetics
Enhancing Your Plots with Colors and Aesthetics

Welcome back! Now that you’ve mastered creating basic scatter plots with ggplot2, it's time to take things up a notch. In this lesson, you'll learn how to add colors and other aesthetic enhancements to your plots. This will help you communicate your data insights more effectively and make your visualizations more engaging.

What You'll Learn

In this unit, you'll learn how to use colors and other aesthetics to differentiate data points in your scatter plots. Specifically, we'll focus on mapping colors to a categorical variable.

By the end of this lesson, you'll be able to generate a scatter plot like the one shown below:

Here's a preview of what you’ll work on:

R
1# Load built-in dataset 2data(iris) 3 4# Scatter plot with colors by Species 5scatter_plot <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) + 6 geom_point() + 7 theme_light()

We'll use the iris dataset again, but this time we'll add more visual clarity by coloring points based on the species of the flowers. This simple enhancement can make a big difference in how data is perceived and interpreted.

Breaking Down the Code

Let’s break down the code snippet provided:

  1. Loading the Dataset:

    R
    1data(iris)

    The iris dataset is a built-in dataset in R, which contains measurements of sepal length and width, petal length and width, for three different species of Iris flowers. This dataset is commonly used in data visualization tutorials due to its simplicity and well-defined categories.

  2. Base ggplot Object:

    R
    1ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species))

    Here, we create the base ggplot object.

    • data = iris specifies the dataset to be used.
    • aes(x = Sepal.Length, y = Sepal.Width, color = Species) maps the Sepal.Length and Sepal.Width columns to the x and y axes, respectively. Additionally, we map the Species column to the color aesthetic, which will differentiate the data points by their species.
  3. Adding Points to the Plot:

    R
    1geom_point()

    This function adds the actual data points to the plot. Each point represents a flower from the dataset, and they are colored according to their species as specified in the aes function.

  4. Applying a Light Theme:

    R
    1theme_light()

    The theme_light() function applies a light theme to the plot, which includes a light background and grid lines, making it visually pleasing while keeping the focus on the data points.

Why It Matters

Adding colors and other aesthetics to your plots helps to emphasize different groups and trends within your data. This makes your plots not only more visually appealing but also more informative. Colors can highlight categories, showcase differences, and enable more complex analyses all at a glance. Mastering aesthetics in ggplot2 equips you with the tools to create clear and compelling visual stories.

Ready for the next step? Let’s start the practice section and inject some color into your plots.

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.