Data Visualization with R and ggplot2

Create a Column Chart

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

Course Sections

Overview

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

Getting started

We’ll use the candy dataset throughout this tutorial. Here’s how the dataset looks:

namesalespriceratingyearcategory
Jelly Beans3002.54.52019Chewy
Gummy Bears1501.53.82020Chewy
Lollipop200142021Hard
Cotton Candy10024.22022Soft
Jolly Ranchers2501.84.72023Hard
Marshmallow1801.23.52024Soft

To view the code to create the candy dataset, click the button below:

What we’ll create

We’ll create a column chart that shows the rating of candy over the years.

Column chart of candy ratings over the years
    Column chart

Steps to create a column chart

Let’s go through the process of creating this column chart step by step.

Step 1: Start a ggplot and specify the data

     ggplot(candy) 
    Base layer

Step 2: Add aesthetics

     ggplot(candy) +
      aes(x = year, y = rating) 
    Aesthetics added
Step 3: Add geometric objects
     ggplot(candy) +
      aes(x = year, y = rating) +
      geom_col() 
    Column chart
Exercise 3.1

Try running the code below to see a column chart with the ratings of candy over the years:

Exercise 3.2

Change the y-axis to sales to create a column chart showing sales over the years.

Step 4: Format axes

Now, let’s improve our column chart by formatting the axes, adding labels, and applying a theme.

We’ll use scale_x_continuous() and scale_y_continuous() to adjust the axes.

The breaks argument in scale_x_continuous() specifies the tick marks on the x-axis, while the limits argument in scale_y_continuous() sets the range of the y-axis.

The position argument in scale_y_continuous() moves the y-axis to the right side.

     ggplot(candy) +
      aes(x = year, y = rating) + 
      geom_col() + 
      scale_x_continuous(breaks = 2019:2024) + 
      scale_y_continuous(limits = c(0, 5), position = "right") 
    Axes formatted

Step 5: Add labels and titles

     ggplot(candy) +
      aes(x = year, y = rating) + 
      geom_col() + 
      scale_x_continuous(breaks = 2019:2024) + 
      scale_y_continuous(limits = c(0, 5), position = "right") +
      labs(alt = "Column chart of year and rating",
           title = "Candy ratings over the years",
           subtitle = "Ratings (1 - 5)",
           caption = "Source: The School of Data", 
           x = NULL, y = NULL) +
      theme(plot.title.position = "plot") 
    Labels and titles added

Step 6: Apply a theme and format text

     ggplot(candy) +
      aes(x = year, y = rating) + 
      geom_col() +
      scale_x_continuous(breaks = 2019:2024) + 
      scale_y_continuous(limits = c(0, 5), position = "right") +
      labs(alt = "Column chart of year and rating",
           title = "Candy ratings over the years",
           subtitle = "Ratings (1 - 5)",
           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.y = element_text(size = 12)) 
    Theme applied and text formatted

Step 7: Add color

Finally, let’s add color to our column chart.

     ggplot(candy) +
      aes(x = year, y = rating) + 
      geom_col(fill = "rosybrown") +
      scale_x_continuous(breaks = 2019:2024) + 
      scale_y_continuous(limits = c(0, 5), position = "right") +
      labs(alt = "Column chart of year and rating",
           title = "Candy ratings over the years",
           subtitle = "Ratings (1 - 5)",
           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)) 
    Final column chart with color
Exercise 3.3

Let’s create a column chart showing the price of candy over the years. Use the code below as a starting point and make the following changes:

  1. Change the y-axis to price
  2. Update the title and subtitle to reflect the new data
  3. Adjust the y-axis limits to fit the price data (hint: use limits = c(0, 3) )
  4. Change the fill color to “skyblue”
Quiz

    Loading...

    Loading...

    Loading...

Review

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

Step 1: Start a ggplot and specify the data frame.

Step 2: Add aesthetics using the aes function.

Step 3: Add geometric objects using geom_col() .

Step 4: Format the axes using scale_x_continuous() and scale_y_continuous() .

Step 5: Add labels and titles using labs() .

Step 6: Apply a theme and format text using theme() .

Step 7: Add color to the chart using the fill argument in geom_col() .

In the next section, we’ll create a line graph in R. Let’s continue learning!