Plotting data with two axes

Generate some data:

set.seed(8-14-2015)

x <- seq(0,1,.1)
n <- c(0,0,1,1,2,3,4,4,5,6,6)
logp <- -log10(runif(length(x)))

Draw the first plot. Leave some room on the right hand side to draw an axis later on.

par(mar = c(5,5,2,5))
plot(x, logp, type="l", col="red3", ylab=expression(-log[10](italic(p))))

Draw the second plot on top of the first (par(new=T)). Draw the plot, but don’t include an axis yet. Put the axis on the right side, and add text to the margin. Add a legend.

par(mar = c(5,5,2,5))
plot(x, logp, type="l", col="red3", ylab=expression(-log[10](italic(p))), ylim=c(0,2))
par(new = T)
plot(x, n, pch=16, axes=F, xlab=NA, ylab=NA)
axis(side = 4)
mtext(side = 4, line = 3, 'Number genes "Expressed"')
legend("topleft",
       legend=c(expression(-log[10](italic(p))), "N genes expressed"),
       lty=c(1,0), pch=c(NA, 16), col=c("red3", "black"))