Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>update</strong>: Copied material that was on the R wiki at <a href="http://rwiki.sciviews.org/doku.php?id=tips:graphics-base:2yaxes" rel="noreferrer">http://rwiki.sciviews.org/doku.php?id=tips:graphics-base:2yaxes</a>, link now broken: also available from <a href="https://web.archive.org/web/20140118053202/http://rwiki.sciviews.org/doku.php?id=tips:graphics-base:2yaxes" rel="noreferrer">the wayback machine</a></p> <h2>Two different y axes on the same plot</h2> <p>(some material originally by Daniel Rajdl 2006/03/31 15:26)</p> <p>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 (<a href="http://junkcharts.typepad.com/junk_charts/2006/06/illusion_of_suc.html" rel="noreferrer">example1</a>, <a href="http://junkcharts.typepad.com/junk_charts/2006/05/the_crossover_l.html" rel="noreferrer">example2</a> from <a href="http://junkcharts.typepad.com/junk_charts/" rel="noreferrer">Junk Charts</a>), as well as <a href="http://www.perceptualedge.com/articles/visual_business_intelligence/dual-scaled_axes.pdf" rel="noreferrer">this article by Stephen Few</a> (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 <a href="http://www.smbc-comics.com/?id=3167#comic" rel="noreferrer">this cartoon</a> ...</p> <p>If you are determined, the basic recipe is to create your first plot, set <code>par(new=TRUE)</code> to prevent R from clearing the graphics device, creating the second plot with <code>axes=FALSE</code> (and setting <code>xlab</code> and <code>ylab</code> to be blank – <code>ann=FALSE</code> should also work) and then using <code>axis(side=4)</code> to add a new axis on the right-hand side, and <code>mtext(...,side=4)</code> to add an axis label on the right-hand side. Here is an example using a little bit of made-up data:</p> <pre><code>set.seed(101) x &lt;- 1:10 y &lt;- rnorm(10) ## second data set on a very different scale z &lt;- runif(10, min=1000, max=10000) par(mar = c(5, 4, 4, 4) + 0.3) # Leave space for z axis plot(x, y) # first plot par(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) </code></pre> <p><code>twoord.plot()</code> in the <code>plotrix</code> package automates this process, as does <code>doubleYScale()</code> in the <code>latticeExtra</code> package.</p> <p>Another example (adapted from an R mailing list post by Robert W. Baer):</p> <pre><code>## set up some fake test data time &lt;- seq(0,72,12) betagal.abs &lt;- c(0.05,0.18,0.25,0.31,0.32,0.34,0.35) cell.density &lt;- c(0,1000,2000,3000,4000,5000,6000) ## add extra space to right margin of plot within frame par(mar=c(5, 4, 4, 6) + 0.1) ## Plot first set of data and draw its axis plot(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 labels mtext("Beta Gal Absorbance",side=2,line=2.5) box() ## Allow a second plot on the same graph par(new=TRUE) ## Plot the second plot and put axis scale on right plot(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 labels mtext("Cell Density",side=4,col="red",line=4) axis(4, ylim=c(0,7000), col="red",col.axis="red",las=1) ## Draw the time axis axis(1,pretty(range(time),10)) mtext("Time (Hours)",side=1,col="black",line=2.5) ## Add Legend legend("topleft",legend=c("Beta Gal","Cell Density"), text.col=c("black","red"),pch=c(16,15),col=c("black","red")) </code></pre> <p><img src="https://i.stack.imgur.com/RPzMi.png" alt="enter image description here"></p> <p>Similar recipes can be used to superimpose plots of different types – bar plots, histograms, etc..</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload