Plotting two y variables on the same plot

Download R script here.

### Create Dummy Data
dates <- seq.dates("1/1/2014", "3/1/2014", by = "days")
y1 <- seq(1, length(dates), 1) + rnorm (length(dates), mean =1, sd= 0.5)
y2 <- rev(seq(1+100, length(dates)+100, 1)) + rnorm (length(dates),
                                                     mean =1, sd= 0.5)

### Create plot element, but don't plot points or axes
par(mar = c(5,4,3,4), mgp = c(1,0.3,0), font=2,
    font.axis = 2, tck = 0.01)
date.range = sort(dates)
y1.range <- c(min(floor(range(y1))), max(ceiling(range(y1))))
plot(dates, y1, ylim = c(y1.range), ann = F, axes = F, type = "n",
     xlim = c(date.range[1], date.range[length(date.range)]))

### Add axes for dates and y1
box()
axis.Date(1, at = seq(date.range[1], date.range[length(date.range)],
                      4), format="%m/%d/%y", labels = F, tck = 0.01)
label=seq(date.range[1], date.range[length(date.range)],4)
x.len=length(label)
text(x=label[1:x.len],  par("usr")[3]-1.35, srt = 45, adj = 1,
     labels = label, xpd = T, cex.axis  = 1.5)
axis(2, at = round(seq(y1.range[1], y1.range[2], 5)), las = 2,
     cex.axis = 1.25)
mtext(side = 2, "Y variable 1", at = 35, line = 2.3, cex = 1.5,
      col = "black")
mtext(side = 1, "Date", cex = 1.5, line = 3.2)

### Add Y1 points
points(dates, y1, pch = 20, cex = 1.5, col = "black")

### Now we add second Y axis and points----------------------------------------#
par(new=T)
y2.range <- c(min(floor(range(y2))), max(ceiling(range(y2))))
plot(dates, y2, ylim = c(y2.range), ann = F, axes = F, type = "n",
     xlim = c(date.range[1], date.range[length(date.range)]))
points(dates, y2, col="red", pch = 20)

### Add 2nd y axis
axis(4, at = round(seq(y2.range[1], y2.range[2], 5)), las = 2,
     cex.axis = 1.25,
     col = "red", col.axis = "red")
mtext(side = 4, "Y variable 2", at = 135, line = 2.3, cex = 1.5,
      col = "red")

### Now we add third Y axis and points----------------------------------------#
y3 <- rep(mean(y2),20) + rnorm (20, mean =10, sd= 5)
y3.dates <- dates[seq(2, 40, 2)] + 13
par(new=T)
y3.range <- c(min(floor(range(y3))), max(ceiling(range(y3))))
plot(dates, y3, ylim = c(y3.range), ann = F, axes = F, type = "n",
     xlim = c(date.range[1], date.range[length(date.range)]))
points(y3.dates, y3, col="magenta", pch = 20)
lines(y3.dates, y3, col="magenta", lwd=2, pch = 20)

### Add 3nd y axis
axis(4, at = round(seq(y2.range[1], y2.range[2], 5)), las = 2, cex.axis = 1.25,
     col = "red", col.axis = "red")
mtext(side = 4, "Y variable 2", at = 135, line = 2.3, cex = 1.5, col = "red")

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s