Implementing ggplot2 Multi-Axis Graphs - How to Apply a Secondary Y-Axis
Want to compare multiple variables in one graph? If you have more than one piece of data, the ggplot2 multi-axis feature makes it easy to visualize them at a glance.
ggplot2 packageIn the How to add a secondary y-axis to analyze data from different perspectivestoday, and we'll break down each step to make it easy for beginners to follow.
Why do I need multiple axes in ggplot2?
In Data Analytics Compare two variables with different unitsFor example, when you want to show data of different nature, such as temperature and precipitation, on one graph, ggplot2 multi-axis is useful.
1. draw a basic line graph
First, let's plot a basic line graph with two pieces of data.
Load the # library
library(ggplot2)
Generate # example data
data <- data.frame(
month = 1:12,
temperature = c(2, 3, 5, 9, 14, 18, 21, 20, 15, 10, 5, 3),
rainfall = c(30, 40, 45, 55, 60, 75, 80, 70, 50, 45, 35, 30)
)
Generate a # base line graph
p <- ggplot(data, aes(x = month)) +
geom_line(aes(y = temperature, color = "Temperature")) +
geom_line(aes(y = rainfall, color = "Rainfall")) +
labs(title = "Monthly Temperature and Rainfall", x = "Month", y = "Temperature")
pCode description:
- aes(x = month): Set the month on the x-axis.
- geom_line(aes(y = temperature, color = "Temperature")): Plots temperature data as a line graph and colors it.
- geom_line(aes(y = rainfall, color = "Rainfall")): Adds precipitation data separately.

2. add a secondary y-axis
Now, the secondary y-axisto show precipitation (y-axis) as a separate axis.
Load the # library
library(ggplot2)
Generate # example data
data <- data.frame(
month = 1:12,
temperature = c(2, 3, 5, 9, 14, 18, 21, 20, 15, 10, 5, 3),
rainfall = c(30, 40, 45, 55, 60, 75, 80, 70, 50, 45, 35, 30)
)
Apply # multi-axis
p <- ggplot(data, aes(x = month)) +
geom_line(aes(y = temperature, color = "Temperature")) +
geom_line(aes(y = rainfall/10, color = "Rainfall")) +
scale_y_continuous(name = "Temperature", sec.axis = sec_axis(~.*10, name = "Rainfall")) +
labs(title = "Monthly Temperature and Rainfall", x = "Month") +
scale_color_manual(values = c("Temperature" = "blue", "Rainfall" = "green"))
pCode description:
- geom_line(aes(y = rainfall/10, color = "Rainfall")): Convert precipitation data to a range similar to temperature by dividing by 10.
- scale_y_continuous(name = "Temperature"", sec.axis = sec_axis(~.*10, name = "Rainfall")): Splits the y-axis in two, and displays precipitation on the right axis, with the left axis representing temperature and the right axis representing precipitation.
- scale_color_manual(values = c("Temperature" = "Temperature", "Rainfall" = "green")): Colorize temperature and rainfall to blue and green, respectively, for better readability.

3. additional example - a polished ggplot2 multi-axis graph
In this example, precipitation is represented by bars and temperature by lines, effectively showing the relationship between the two variables. The color contrast and improved layout make it easy to see patterns in the data, and the professional, sleek design enhances the visual appeal.
Load the # library
library(ggplot2)
library(scales)
Generate # example data
data <- data.frame(
month = 1:12,
temperature = c(2, 3, 5, 9, 14, 18, 21, 20, 15, 10, 5, 3),
rainfall = c(30, 40, 45, 55, 60, 75, 80, 70, 50, 45, 35, 30)
)
Generate a # enhanced multi-axis graph
p <- ggplot(data, aes(x = month)) +
geom_col(aes(y = rainfall, fill = "Rainfall"), alpha = 0.5) +
geom_line(aes(y = temperature * 4, color = "Temperature"), size = 1.2) +
geom_point(aes(y = temperature * 4, color = "Temperature"), size = 3) +
scale_y_continuous(
name = "Rainfall (mm)",
sec.axis = sec_axis(~./4, name = "Temperature (°C)")
) + Β
scale_x_continuous(breaks = 1:12, labels = month.abb) +
scale_fill_manual(values = c("Rainfall" = "#69b3a2")) +
scale_color_manual(values = c("Temperature" = "#E69F00")) +
labs(
title = "Monthly Temperature and Rainfall",
subtitle = "Showcasing climate patterns throughout the year",
x = "Month",
color = "",
fill = ""
) + Β
theme_minimal() +
theme(
legend.position = "top",
plot.title = element_text(hjust = 0.5, face = "bold", size = 16),
plot.subtitle = element_text(hjust = 0.5, size = 12, color = "gray50"),
axis.title = element_text(face = "bold"),
panel.grid.major = element_line(color = "gray90"),
panel.grid.minor = element_blank()
)
pCode description:
geom_col(): Represents precipitation as a bar graph.alpha = 0.5to adjust the transparency.geom_line()andgeom_point(): Represent the temperature as a line graph and points. We multiplied the temperature values by 4 to scale them similarly to precipitation.scale_y_continuous(): The primary y-axis represents precipitation and the secondary y-axis represents temperature.sec.axis = sec_axis(~./4, name = "Temperature (°C)")for the secondary axis.scale_x_continuous(): Shows the x-axis labels as month abbreviations.scale_fill_manual()andscale_color_manual(): Assign different colors to precipitation and temperature respectively.labs(): Set the title, subtitle, and axis labels for the graph.theme_minimal(): Apply a clean default theme.theme(): Adjust the detailed styling of the graph. I customized the legend position, title style, grid lines, and more.

Wrap-up: Compare data at a glance with ggplot2 multiple axes!
With ggplot2 multiple axes, you can visualize and compare data in different units on one graph. The secondary y-axis makes it easier to interpret complex data, which is a great addition to your analysis.
Now you too can represent a variety of data in a single graph. In the next post, we'll look at the Introduction to advanced theme settings for ggplot2We will, so stay tuned!





