In this guide, we’ll show you how to add an extra axis in ggplot2. This is useful when you want to display additional data that is related to your plot, but not directly part of the plot itself. We’ll go over the steps to add a secondary y-axis, as well as a secondary x-axis. By the end of this guide, you’ll be able to add extra axes to your ggplot2 plots with confidence.
Adding a Secondary Y-Axis
To add a secondary y-axis to a ggplot2 plot, you can use the scale_y_continuous() function with the sec.axis() argument. Here’s an example:
# Load the ggplot2 library
library(ggplot2)
# Create a sample dataset
data <- data.frame(x = 1:10, y1 = 1:10, y2 = 10:1)
# Create a ggplot2 plot with two y-axes
ggplot(data, aes(x = x)) +
geom_line(aes(y = y1), color = "blue") +
geom_line(aes(y = y2), color = "red") +
scale_y_continuous(name = "y1",
sec.axis = sec_axis(~ . * max(data$y1) / max(data$y2), name = "y2")) +
labs(color = "Variable")
In this example, we created a sample dataset with two y-variables (y1 and y2). We then created a ggplot2 plot with two y-axes, one for each variable. The scale_y_continuous() function is used to specify the name of the primary y-axis (y1), as well as the transformation function to map the secondary y-axis (y2) to the primary y-axis (y1). The sec_axis() function is used to specify the name of the secondary y-axis (y2).
Adding a Secondary X-Axis
To add a secondary x-axis to a ggplot2 plot, you can use the scale_x_continuous() function with the sec.axis() argument. Here’s an example:
# Load the ggplot2 library
library(ggplot2)
# Create a sample dataset
data <- data.frame(x1 = 1:10, x2 = 10:1, y = 1:10)
# Create a ggplot2 plot with two x-axes
ggplot(data, aes(x = x1)) +
geom_line(aes(y = y), color = "blue") +
geom_line(aes(x = x2, y = y), color = "red") +
scale_x_continuous(name = "x1",
sec.axis = sec_axis(~ . * max(data$x1) / max(data$x2), name = "x2")) +
labs(color = "Variable")
In this example, we created a sample dataset with two x-variables (x1 and x2). We then created a ggplot2 plot with two x-axes, one for each variable. The scale_x_continuous() function is used to specify the name of the primary x-axis (x1), as well as the transformation function to map the secondary x-axis (x2) to the primary x-axis (x1). The sec_axis() function is used to specify the name of the secondary x-axis (x2).
Adding Both a Secondary X-Axis and a Secondary Y-Axis
To add both a secondary x-axis and a secondary y-axis to a ggplot2 plot, you can use both the scale_x_continuous() and scale_y_continuous() functions with the sec.axis() argument. Here’s an example:
# Load the ggplot2 library
library(ggplot2)
# Create a sample dataset
data <- data.frame(x1 = 1:10, x2 = 10:1, y1 = 1:10, y2 = 10:1)
# Create a ggplot2 plot with two x-axes and two y-axes
ggplot(data, aes(x = x1)) +
geom_line(aes(y = y1), color = "blue") +
geom_line(aes(x = x2, y = y2), color = "red") +
scale_x_continuous(name = "x1",
sec.axis = sec_axis(~ . * max(data$x1) / max(data$x2), name = "x2")) +
scale_y_continuous(name = "y1",
sec.axis = sec_axis(~ . * max(data$y1) / max(data$y2), name = "y2")) +
labs(color = "Variable")
In this example, we created a sample dataset with two x-variables (x1 and x2) and two y-variables (y1 and y2). We then created a ggplot2 plot with two x-axes and two y-axes. The scale_x_continuous() and scale_y_continuous() functions are used to specify the name of the primary x-axis and y-axis (x1 and y1), respectively, as well as the transformation functions to map the secondary x-axis (x2) and secondary y-axis (y2) to the primary x-axis (x1) and primary y-axis (y1), respectively. The sec_axis() function is used to specify the name of the secondary x-axis (x2) and secondary y-axis (y2).
In this guide, we showed you how to add an extra axis in ggplot2. By using the scale_x_continuous() and scale_y_continuous() functions with the sec.axis() argument, you can easily add a secondary x-axis, a secondary y-axis, or both to your ggplot2 plots. With this knowledge, you can create more informative and visually appealing plots that can help you better understand your data.
References
| Title | Link |
|---|---|
| How to Add a Secondary Y-Axis in ggplot2 | https://www.r-graph-gallery.com/25-ggplot2-secondary-axis-y.html |
| How to Add a Secondary X-Axis in ggplot2 | https://www.r-graph-gallery.com/26-ggplot2-secondary-axis-x.html |
| How to Add Both a Secondary X-Axis and a Secondary Y-Axis in ggplot2 | https://www.r-graph-gallery.com/27-ggplot2-secondary-axis-x-y.html |