Two different y axes on the same plot

来源:互联网 发布:淘宝详情装修 编辑:程序博客网 时间:2024/06/05 10:21

(some material originally by Daniel Rajdl 2006/03/31 15:26)

http://stackoverflow.com/questions/6142944/how-can-i-plot-with-2-different-y-axes-in-r?lq=1

Please note that there are very few situations where it is appropriate to use two different scales on the same plot. It is very easy to mislead the viewer of the graphic. Check the following two examples and comments on this issue (example1,example2 from Junk Charts, as well as this article by Stephen Few (which concludes “I certainly cannot conclude, once and for all, that graphs with dual-scaled axes are never useful; only that I cannot think of a situation that warrants them in light of other, better solutions.”)  Also see point #4 in this cartoon ...

If you are determined, the basic recipe is to create your first plot, set par(new=TRUE) to prevent R from clearing the graphics device, creating the second plot withaxes=FALSE (and setting xlab and ylab to be blank –ann=FALSE should also work) and then using axis(side=4) to add a new axis on the right-hand side, andmtext(...,side=4) to add an axis label on the right-hand side. Here is an example using a little bit of made-up data:

set.seed(101)x <- 1:10y <- rnorm(10)## second data set on a very different scalez <- runif(10, min=1000, max=10000) par(mar = c(5, 4, 4, 4) + 0.3)  # Leave space for z axisplot(x, y) # first plotpar(new = TRUE)plot(x, z, type = "l", axes = FALSE, bty = "n", xlab = "", ylab = "")axis(side=4, at = pretty(range(z)))mtext("z", side=4, line=3)

twoord.plot() in the plotrix package automates this process, as doesdoubleYScale() in the latticeExtra package.

Another example (adapted from an R mailing list post by Robert W. Baer):

## set up some fake test datatime <- seq(0,72,12)betagal.abs <- c(0.05,0.18,0.25,0.31,0.32,0.34,0.35)cell.density <- c(0,1000,2000,3000,4000,5000,6000)## add extra space to right margin of plot within framepar(mar=c(5, 4, 4, 6) + 0.1)## Plot first set of data and draw its axisplot(time, betagal.abs, pch=16, axes=FALSE, ylim=c(0,1), xlab="", ylab="",    type="b",col="black", main="Mike's test data")axis(2, ylim=c(0,1),col="black",las=1)  ## las=1 makes horizontal labelsmtext("Beta Gal Absorbance",side=2,line=2.5)box()## Allow a second plot on the same graphpar(new=TRUE)## Plot the second plot and put axis scale on rightplot(time, cell.density, pch=15,  xlab="", ylab="", ylim=c(0,7000),     axes=FALSE, type="b", col="red")## a little farther out (line=4) to make room for labelsmtext("Cell Density",side=4,col="red",line=4) axis(4, ylim=c(0,7000), col="red",col.axis="red",las=1)## Draw the time axisaxis(1,pretty(range(time),10))mtext("Time (Hours)",side=1,col="black",line=2.5)  ## Add Legendlegend("topleft",legend=c("Beta Gal","Cell Density"),  text.col=c("black","red"),pch=c(16,15),col=c("black","red"))

enter image description here

As its name suggests, twoord.plot() in the plotrix package plots with two ordinate axes.

library(plotrix)example(twoord.plot)

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

0 0