Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Especially when analyzing time series, we often need plots with two vertical axes. Researchers often expect the two series to \”move together,\” but with different locations and scales. To show that the series move together, you should give each series its own scale. One vertical scale should appear on the left side of the plot and the other on the right. In this post, I\’ll show you how to add the extra vertical axis on the right side of the plot.
Generating Sample Data
Let\’s start by quickly generating two fake time series. This can be done with the code below.
set.seed(3571)
n <- 100
a1 <- rnorm(n)
a2 <- rnorm(n)
x <- NULL
y <- NULL
x[1] <- a1[1]
y[1] <- a2[1]
for (t in 2:n) {
x[t] <- -.2*x[t-1] + .5*y[t-1] + a1[t]
y[t] <- y[t-1] + a2[t]
}
The Problem
Now let\’s plot the data in the \”standard way\”: plot one series using the high-level plot()
function and then add the other series using the low-level lines()
function.
plot(ts(x))
lines(ts(y), col = \"red\")
This code gives the plot below.
You can see one big problem–the second series goes off the scale! (We could fix this particular problem by plotting the series in the opposite order, but that does not work in general.)
A second problem is that the scales of the series are different. Both series are heading to infinity, but the x series is going there more slowly. The x series also has less variability than the y series. Both of these differences suggest we plot the series on different vertical scales. Indeed, our hypothesis usually suggests that the series should \”move together,\” not that particular values of x should correspond to the same values of y.
The Solution
You can easily add a separate vertical scale using the par(new=TRUE)
command between two calls to the high-level plot()
function. The code below does just this.
plot(ts(x))
par(new = T)
plot(ts(y), col = \"red\")
This code produces the figure below.
We can see from this plot that the two series indeed move together. Also notice two details of this plot. First, the vertical axes (one is plotted over the other on the left side) have different scales. Second, the horizontal axes are on the same scale (you can tell because R is plotting the exact same axis twice, making the axis fuzzy).
Now we just need to clean up the plot a little, moving one axis to the right side of the figure, plotting the horizontal axis only once, and cleaning up the labeling a little. Note that R defaults for axes and axis notation are not the best, but I deal with that in a separate post. For this exercise, I use the R defaults when they make sense, even if they are not the most aesthetically pleasing.
I make the following changes.
- Adjust the
mar
option in the graphical parameters to give me a little more room for constructing the vertical axis on the right side. - Add the
xlab=\"x\"
argument to the firstplot()
function call. This improves the label of the vertical axis on the left. - Add the
axes = F
,xlab = NA
, andylab = NA
arguments to the secondplot()
function call. This removes all the axes and axis notation from this call. - Add the vertical axis on the right with a simple call to the
axis()
function and settingside = 4
. - Add a label to the vertical axis on the right with the
mtext()
command. Use the optionsside = 4
andline = 3
to position the text\"y\"
correctly.
The new code is given below.
par(mar = c(5,5,2,5))
plot(ts(x), ylab = \"x\")
par(new = T)
plot(ts(y), col = \"red\", axes = F, xlab = NA, ylab = NA)
axis(side = 4)
mtext(side = 4, line = 3, \"y\")
This code produces the following plot, which could be cleaned up a lot, but has the basic feature we\’re looking for: a separate vertical axis for each series.
Summary
In this post, I\’ve shown how to add a separate vertical axis for each series in a plot. This can be accomplished by setting the parameter new=\"TRUE\"
after plotting the first series. I\’ve focused on solving the main problem at the expense of nicer axes and axis notation. I\’ve written more about how to improve on R\’s axis defaults here and here.
I encourage you to share this with others and contribute to the conversation at How to Add an Extra Vertical Axis to R Plots, which first appeared at carlislerainey.com.For more of my thoughts and ideas, subscribe to my blog (via RSS or Email) and follow me on Twitter. You also might like to browse my archive and read my papers on Strategic Mobilization and Testing Hypotheses of No Meaningful Effect.
You might also find these posts useful:
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.