Making dual y-axis graphs in R
(without additional packages)

This note is inspired by the blog entry of Yan Holtz on r-graph-gallery.com on how to create a dual y-axis chart. While Yan uses ggplot2 to achieve this, this note shows you how to create a dual y-axis graph without installing another package (even though ggplot2 is admittedly a fantastic and popular package).

If you've ever needed to visualize two different datasets with different scales in R, you might have come across the challenge of creating a dual y-axis chart. While there are specialized packages for this task, you can achieve a dual-axis plot without installing any additional packages. In this blog post, we'll walk through a step-by-step guide using a simple example.

Step 1: Define the plot region

The first step is to define the plot region. This determines the size and margins of our chart. In R, the par function allows us to configure various parameters, including margins, which significantly influence the size and layout of our chart.

par(mar = c(6, 4, 2, 4) + 0.1)

In this step, we focus on adjusting the margins to ensure an optimal layout. The mar argument in the par function controls the margins in the order of bottom, left, top, and right. For instance:

By fine-tuning these margins, we create a visually appealing and well-proportioned canvas for our dual y-axis chart.

Step 2: Plot the first graph

The first step is to define the plot region. This determines the size and margins of our chart. In R, the par function allows us to configure various parameters, including margins, which significantly influence the size and layout of our chart.

# Plot the first graph
plot(var1,

     time_var,

     col = "blue",

     type = "l",

     ylab = "Variable 1",

     xlab = "Year",

     lwd = 1.5)

In this example, we use the plot function to generate a line plot of our first variable, var1, against the time variable, time_var. The parameters such as colour (col), line type (type), and line width (lwd) contribute to the clarity and visual appeal of the plot, as you can see in the following graph:

Step 3: Overlaying the second data set

To transform our single-axis plot into a dual y-axis chart, we overlay the initial graph with another one. Let's consider adding the var2 data to our existing chart:

# Enable to lay over another plot
par(new = TRUE)


# Create a second plot
plot(time_var,

     var2,

     col = "red",

     lwd = 1.5,

     type = "l",

     ylab = "",

     xlab = "",

     axes = FALSE)

By utilizing the par function with the new parameter, we can overlay the var2 plot onto the existing chart, preparing the groundwork for the dual y-axis display. This results in a plot similiar to the one below:

Step 4: Adding the second y-axis

With both plots overlaid, we now need to introduce a second y-axis specifically for the var2 variable:

# Add a second axis to the right (4 = y-axis on RHS)
axis(4, col = "black")


# Give it a name and adjust the scale
mtext("Variable 2",

      side = 4,

      line = 3)

The axis function specifies that we want to add a new axis to the right-hand side of the plot (4), and that R should make it black. Moreover, the mtext function is employed to label the second y-axis with the variable name "Variable 2". The side parameter indicates the side of the plot where the label should appear (4 corresponds to the right side), and line controls the distance of the label from the axis. This should yield a plot akin to the one below:

Step 5: Providing context with a legend

Great! But we also want to ensure that other people understand what line refers to which axis. Thus, to enhance the interpretability of our dual y-axis chart, we should include a legend at the bottom, providing a clear reference for the plotted lines.

# Add a legend
legend("bottom",

       legend = c("Variable 1", "Variable 2"),

       col = c("blue", "red"),

       lty = 1,

       lwd = 1.5,

       bty = "n",

       cex = 1.1,

       xpd = TRUE,

       horiz = TRUE,

       inset = c(0, -0.4),

       xjust = 0.5)

The legend function allows us to specify the labels, colors, and line styles, ensuring that the audience can easily differentiate between the two variables. Then, you should obtain something like this:

Congratulations! You've successfully created a dual y-axis chart in R without the need for additional packages. This technique is particularly useful when comparing datasets with different scales, providing a clear and concise visualization. Feel free to adapt this approach for your own datasets, and happy plotting!