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:
How to create a column chart How to Create a Column Chart
A column chart is used to compare values across categories, displaying data as vertical bars.
show code 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 ))
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2"> Step 1: Create a base layer
Start with the ggplot
function and specify the data frame.
show code ggplot( candy )
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2"> Use the aes
function to map the variables (x and y axes).
show code ggplot( candy ) +
aes( x = year, y = rating )
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2"> Step 3: Add geometric objects
Use geom_col()
to create the column chart.
show code ggplot( candy ) +
aes( x = year, y = rating ) +
geom_col()
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2"> Adjust the x and y axes for better readability.
show code ggplot( candy ) +
aes( x = year, y = rating ) +
geom_col() +
scale_x_continuous( breaks = 2019 : 2024 ) +
scale_y_continuous( limits = c ( 0 , 5 ) , position = "right" )
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2"> Step 5: Add labels and titles
Add alt text, title, subtitle, and caption to the plot.
show code 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" )
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2"> Step 6: Apply a theme and format text
Use a minimal theme and customize text appearance.
show code 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 ))
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2"> Customize the color of the columns and make final adjustments.
show code 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 ))
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2">
Getting started
We’ll use the candy
dataset throughout this tutorial. Here’s how the dataset looks:
name sales price rating year category Jelly Beans 300 2.5 4.5 2019 Chewy Gummy Bears 150 1.5 3.8 2020 Chewy Lollipop 200 1 4 2021 Hard Cotton Candy 100 2 4.2 2022 Soft Jolly Ranchers 250 1.8 4.7 2023 Hard Marshmallow 180 1.2 3.5 2024 Soft
To view the code to create the candy
dataset, click the button below:
show code to create candy dataset 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" )
)
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2">
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
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 )
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2">
Step 2: Add aesthetics
Step 3: Add geometric objects
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.
show solution ggplot( candy ) +
aes( x = year, y = sales ) +
geom_col()
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2">
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" )
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2">
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" )
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2">
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 ))
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2">
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 ))
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2">
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:
Change the y-axis to price
Update the title and subtitle to reflect the new data
Adjust the y-axis limits to fit the price data (hint: use limits = c(0, 3)
)
Change the fill color to “skyblue”
show solution ggplot( candy ) +
aes( x = year, y = price ) +
geom_col( fill = "skyblue" ) +
scale_x_continuous( breaks = 2019 : 2024 ) +
scale_y_continuous( limits = c ( 0 , 3 ) , position = "right" ) +
labs( alt = "Column chart of year and price" ,
title = "Candy prices over the years" ,
subtitle = "Price in dollars" ,
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 ))
copied = true)" @mouseleave.debounce.1000ms="copied && (copied = false)" @keydown.enter.debounce.1000ms="copied && (copied = false)" @keydown.space.debounce.1000ms="copied && (copied = false)" @touchstart.debounce.1000ms="copied && (copied = false)" class="group flex select-none items-center justify-between gap-2 rounded p-2 hover:bg-3 hover:text-brand focus-visible:outline-none focus-visible:ring-1 motion-safe:transition-colors absolute right-0 top-0 text-2">
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!