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:
How to create a line chart How to Create a Line Chart
A line chart is an effective way to visualize trends over time or continuous data.
show code 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 ))
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 = sales )
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_line()
to create the line and geom_point()
to add data points.
show code ggplot( candy ) +
aes( x = year, y = sales ) +
geom_line() +
geom_point()
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 x and y axes for better readability.
show code 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" )
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 = 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 )
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 and modify a theme to improve the overall look of the chart.
show code 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( panel.grid.major.x = element_blank() ,
panel.grid.minor.x = element_blank() ,
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 7: Customize line and points
Adjust the appearance of the line and points for better visibility.
show code 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 ))
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 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
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">
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
Step 1: Add Data Layer
Let’s start by creating the base plot with our data:
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 )
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"> 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()
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"> This creates a basic line chart with points for each data point.
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" )
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"> 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" )
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"> 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 ))
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"> 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 ))
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"> 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.
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.