# [R] overlay plots

Duncan Murdoch murdoch at stats.uwo.ca
Thu Sep 6 17:36:40 CEST 2001

```On Thu, 06 Sep 2001 15:08:16 +0100, you wrote in message
<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.

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

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

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

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

plot(y ~ x)
points(z ~ x)

Because the first call establishes the coordinates, you might find
that not all the z values are shown.  To guarantee this, you can use
optional arguments in the first call, e.g.

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

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

Duncan Murdoch
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```