Data Visualization with R and ggplot2

Create a Line Chart

Learn how to create a line chart in R with ggplot2.

Course Sections

Overview

In this tutorial, we’ll learn how to create a line chart in R using the ggplot2 package. Here is a brief overview of what we’ll cover:

What is a Line Chart?

A line chart is a type of graph that displays information as a series of data points connected by straight line segments. It is particularly useful for showing trends over time.

Creating Sample Data

Let’s start by creating a sample dataset of candy sales over the years:

Sample Data
     candy <- data.frame(
      name = c("Jelly Beans", "Gummy Bears", "Lollipop", "Cotton Candy", "Jolly Ranchers", "Marshmallow"),
      sales = c(300, 150, 200, 100, 250, 180),
      price = c(2.5, 1.5, 1.0, 2.0, 1.8, 1.2),
      rating = c(4.5, 3.8, 4.0, 4.2, 4.7, 3.5),
      year = c(2019:2024),
      category = c("Chewy", "Chewy", "Hard", "Soft", "Hard", "Soft")
    )
     
    candy 
    namesalespriceratingyearcategory
    Jelly Beans3002.54.52019Chewy
    Gummy Bears1501.53.82020Chewy
    Lollipop200142021Hard
    Cotton Candy10024.22022Soft
    Jolly Ranchers2501.84.72023Hard
    Marshmallow1801.23.52024Soft

Step 1: Add Data Layer

Let’s start by creating the base plot with our data:

Data Layer
     ggplot(candy) 
    An empty plot area

    This creates an empty plot area.

Step 2: Add Aesthetic Layer

Next, we’ll specify which variables to use for the x and y axes:

Aesthetic Layer
     ggplot(candy) +
      aes(x = year, y = sales) 
    A plot area with x and y axes defined

    This defines the x and y axes but doesn’t plot any data yet.

Step 3: Add Geom Layer

Now we’ll add the line and points to our chart:

Geom Layer
     ggplot(candy) +
      aes(x = year, y = sales) +
      geom_line() +
      geom_point() 
    A basic line chart with points

    This creates a basic line chart with points for each data point.

Step 4: Format Axis

Let’s format the x and y axes:

Axis Formatting
     ggplot(candy) +
      aes(x = year, y = sales) + 
      geom_line() +
      geom_point() +
      scale_x_continuous(breaks = 2019:2024) + 
      scale_y_continuous(limits = c(0, 350), position = "right") 
    A line chart with formatted axes

    This sets specific breaks for the x-axis and limits for the y-axis, moving the y-axis to the right side.

Step 5: Add Text

Now let’s add titles and labels to our chart:

Adding Text
     ggplot(candy) +
      aes(x = year, y = sales) + 
      geom_line() +
      geom_point() +
      scale_x_continuous(breaks = 2019:2024) + 
      scale_y_continuous(limits = c(0, 350), position = "right") +
      labs(alt = "Line chart of candy sales over years",
           title = "Candy sales fluctuate over time",
           subtitle = "Annual sales (in units)",
           caption = "Source: The School of Data", 
           x = NULL, y = NULL) +
      theme(plot.title.position = "plot") 
    A line chart with titles and labels

    This adds a title, subtitle, caption, and alt text to our chart.

Step 6: Format Text

Let’s improve the formatting of our text:

Text Formatting
     ggplot(candy) +
      aes(x = year, y = sales) + 
      geom_line() +
      geom_point() +
      scale_x_continuous(breaks = 2019:2024) + 
      scale_y_continuous(limits = c(0, 350), position = "right") +
      labs(alt = "Line chart of candy sales over years",
           title = "Candy sales fluctuate over time",
           subtitle = "Annual sales (in units)",
           caption = "Source: The School of Data", 
           x = NULL, y = NULL) +
       theme_minimal() +
       theme(text = element_text(family = "PT Sans"),
           panel.grid.major.x = element_blank(),
           panel.grid.minor.x = element_blank(),
           plot.title.position = "plot",
           plot.title = element_text(face = "bold", size = 16),
           plot.subtitle = element_text(face = "italic", 12),  
           axis.text = element_text(size = 12)) 
    A line chart with formatted text

    This applies a minimal theme and customizes the text appearance.

Step 7: Customize Line and Points

Finally, let’s customize the appearance of our line and points:

Customizing Line and Points
     ggplot(candy) +
      aes(x = year, y = sales) + 
      geom_line(color = "steelblue", size = 1) +
      geom_point(color = "steelblue", size = 3) +
      scale_x_continuous(breaks = 2019:2024) + 
      scale_y_continuous(limits = c(0, 350), position = "right") +
      labs(alt = "Line chart of candy sales over years (2019: 300, 2020: 150, 2021: 200, 2022: 100, 2023: 250, 2024: 180)",
           title = "Candy sales fluctuate over time",
           subtitle = "Annual sales (in units)",
           caption = "Source: The School of Data", 
           x = NULL, y = NULL) +
       theme_minimal() +
       theme(text = element_text(family = "PT Sans"),
           panel.grid.major.x = element_blank(),
           panel.grid.minor.x = element_blank(),
           plot.title.position = "plot",
           plot.caption.position = "plot",
           plot.title = element_text(face = "bold", size = 16),
           plot.subtitle = element_text(face = "italic", 12),  
           axis.text = element_text(size = 12)) 
    A final customized line chart

    This customizes the color and size of the line and points, completing our line chart.

Exercise

Try creating a line chart using the candy dataset, plotting the price over the year . Customize the colors and add appropriate labels.

Line Chart Exercise
Quiz

    Loading...

    Loading...

    Loading...

Review

In this tutorial, we learned how to create a line chart in R using the ggplot2 package. We covered the following steps:

Step 1: Create a base layer with the ggplot function.

Step 2: Add aesthetics using the aes function.

Step 3: Add geometric objects like lines and points using geom_line and geom_point .

Step 4: Format the x and y axes using scale_x_continuous and scale_y_continuous .

Step 5: Add labels and titles to the plot using the labs function.

Step 6: Apply a theme to improve the overall look of the chart.

Step 7: Customize the line and points for better visibility.

In the next section, we’ll create a scatter plot using a similar approach.