Draw Plot with Two Y-Axes in R (Example)
In this R programming tutorial you’ll learn how to create a plot with two y-axes in Base R.
Table of contents:
Here’s how to do it:
Creation of Example Data
First, we need to create some example data for the plot of this example:
set.seed(25852) # Create example data x <- rnorm(30) y1 <- x + rnorm(30) y2 <- x + rnorm(30, 5) |
set.seed(25852) # Create example data x <- rnorm(30) y1 <- x + rnorm(30) y2 <- x + rnorm(30, 5)
Our example data consists of three numeric vectors: x, y1, and y2.
Now, let’s plot these data!
Example: Create Plot with 2 Axes in R
If we want to draw a plot with two different y-axes, we can use the following R code:
par(mar = c(5, 4, 4, 4) + 0.3) # Additional space for second y-axis plot(x, y1, pch = 16, col = 2) # Create first plot par(new = TRUE) # Add new plot plot(x, y2, pch = 17, col = 3, # Create second plot without axes axes = FALSE, xlab = "", ylab = "") axis(side = 4, at = pretty(range(y2))) # Add second axis mtext("y2", side = 4, line = 3) # Add second axis label |
par(mar = c(5, 4, 4, 4) + 0.3) # Additional space for second y-axis plot(x, y1, pch = 16, col = 2) # Create first plot par(new = TRUE) # Add new plot plot(x, y2, pch = 17, col = 3, # Create second plot without axes axes = FALSE, xlab = "", ylab = "") axis(side = 4, at = pretty(range(y2))) # Add second axis mtext("y2", side = 4, line = 3) # Add second axis label
Figure 1: Plot with 2 Y-Axes in R.
Figure 1 is illustrating the output of the previous R syntax. As you can see, we created a scatterplot with two different colors and different y-axis values on the left and right side of the plot.
How did we do that? Let’s deconstruct the code:
- par(mar = c(5, 4, 4, 4) + 0.3) – This code defines how much white space should be shown around the plot. It is important to leave enough space for the second y-axis.
- plot(x, y1, pch = 16, col = 2) – This code creates the first plot (i.e. the red dots).
- par(new = TRUE) – This code specifies that we want to overlay a second plot.
- plot(x, y2, pch = 17, col = 3, axes = FALSE, xlab = “”, ylab = “”) – This code draws the second plot (i.e. the green triangles).
- axis(side = 4, at = pretty(range(y2))) – This code adds the axis labels on the right side.
- mtext(“y2”, side = 4, line = 3) – This code adds the name of the second y-axis (i.e. y2).
Note that this code is adapted from this thread on Stack Overflow. Have a look at this thread for more examples of the plotting of multiple axes.
Also note that we used a scatterplot for this example. However, you may add a secondary axis to many different types of graphics such as boxplots, line charts, histograms, density plots, and so on…
Video, Further Resources & Summary
I have recently released a video on my YouTube channel, which explains the R programming code of this tutorial. You can find the video below:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you might want to have a look at the related tutorials on my homepage:
- Remove Axis Values of Plot in Base R
- Rotate Axis Labels of Base R Plot
- The par Function in R
- R Graphics Gallery
- The R Programming Language
In summary: This article explained how to add dual axes to a plot in the R programming language. Don’t hesitate to let me know in the comments, in case you have further comments or questions.
Subscribe to my free statistics newsletter: