[R] overlay plots

Martin Maechler maechler at stat.math.ethz.ch
Fri Sep 7 09:02:51 CEST 2001


>>>>> "Duncan" == Duncan Murdoch <murdoch at stats.uwo.ca> writes:

    Duncan> On Thu, 06 Sep 2001 15:08:16 +0100, you wrote in message
    Duncan> <OF62D87A41.4733FB80-ON80256ABF.004D2E97 at cso.ie>:

    >> Hi all!
    >> 
    >> I new to R (I don't know anything about S+ either!)
    >> 
    >> I've a simple question:
    >> 
    >> How do I generate overlay plots in R?
    >> So far as I can see the plot(x, y) operator will only give me one graph and
    >> the plot(x ~ y + z) will give me 2 separate plots.
    >> Is there an easy way to overlay or am I missing the obvious?
    >> 
    >> 
    >> Any help welcome.

    Duncan> The general scheme in R (and S+) is that one call sets up a
    Duncan> plotting region and establishes a coordinate system, and
    Duncan> possibly fills it, and then further calls can be used to
    Duncan> embellish it.

    Duncan> What you want to do is to use one call to plot y vs. x, then
    Duncan> add the z vs. x points to that plot.  A simple way to do that
    Duncan> is

    Duncan> plot(x,  y)  
    Duncan> points(x, z)

    Duncan> Note that the horizontal coordinate is listed first when you
    Duncan> list arguments, but the vertical coordinate is listed first
    Duncan> when you use a formula, so this is equivalent:

    Duncan> plot(y ~ x)
    Duncan> points(z ~ x)
 
    Duncan> Because the first call establishes the coordinates, you might
    Duncan> find that not all the z values are shown.  To guarantee this,
    Duncan> you can use optional arguments in the first call, e.g.

    Duncan> plot(x, y, ylim = range(c(y,z)))

    Duncan> You should also look at "axis", "lines", "text", "mtext", "box" etc.
    Duncan> for other functions to embellish the plot.

But if it's really as simple, you might also think of using
matplot(), matlines(),...  which are useful wrappers to
one plot(.) + several points() / lines() calls.  For the above example,

    matplot(x, cbind(y,z))

Martin Maechler <maechler at stat.math.ethz.ch>
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list