[R] Plotting two lines on a graph when using par(mfrow=)

Joshua Wiley jwiley.psych at gmail.com
Sun Feb 27 23:23:48 CET 2011


Hi Eric,

On Sun, Feb 27, 2011 at 10:06 AM, eric <ericstrom at aol.com> wrote:
> Basic question but still learning ....
>
> How do I plot two lines (f$equity and f$bh.equity) on one of the three
> graphs under mfrow ? I tried putting brackets around the first plot and
> lines command but that didn't work.
>
> par(mfrow=c(3,1))
> {plot(f$Date,f$equity, col="blue", type="l", main="equity")
> lines(f$bh.equity, col="gray")}

When you only specify one set of coordinates, it is passed to the y
axis, and the default x values are basically just an index from 1 to
length(ycoords).  Consider this output for example:

plot(11:20)

now assuming that the values in f$Date are greater than the automatic
index created for bh.equity, the line is essentially out of view.  The
y coordinates may be in the range of your plot but the x coordinates
are not.  Of course, I am guessing to an extent here because I do not
have your actual data.  This is particularly likely if f$Date is
actually a date class object because R represents dates internally as
the number of days since 1978, I believe.  You do not need the curly
braces.  Try something like:

lines(f$Date, f$bh.equity, col="gray")

and that ought to work.  If not, consider whether the values in
f$bh.equity are within the same range as f$equity, if not, you may
need to explicitly set the y limits in your original plot() call (see
the ylim argument to do that).

HTH,

Josh

> plot(f$Date,f$indicator, col="green", type="l", main="indicator")
> plot(f$Date, f$SPY, col="red", type="l", main="SPY")
>
> What I want is the first graph to have two lines(equity and bh.equity), then
> the next two graphs to have one line each.
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Plotting-two-lines-on-a-graph-when-using-par-mfrow-tp3326979p3326979.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list