[R] Plotting multiple columns on same graph

Achim Zeileis zeileis at ci.tuwien.ac.at
Fri Feb 8 22:00:56 CET 2002


Andrew Perrin wrote:
> 
> I'd like to produce a series of simple line graphs for my methods class
> that show the three questions used on a repeated survey to make up a
> particular index.  The data frame is:
> 
> > efficacy.df
>    year complicated havesay dontcare
> 1  1952          71      68       63
> 2  1954          NA      NA       NA
> 3  1956          64      71       71
> 4  1958          NA      NA       NA
> 5  1960          59      72       73
> 6  1962          NA      NA       NA
> 7  1964          67      70       62
> 8  1966          69      60       57
> 9  1968          71      58       55
> 10 1970          73      64       50
> 11 1972          74      59       49
> 12 1974          72      57       46
> 13 1976          70      56       44
> 14 1978          NA      53       45
> 15 1980          71      59       43
> 16 1982          NA      52       50
> 17 1984          70      68       57
> 18 1986          66      NA       43
> 19 1988          66      50       37
> 20 1990          65      34       23
> 21 1992          63      57       37
> 22 1994          73      34       22
> 23 1996          60      38       24
> 24 1998          71      46       25
> 25 2000          NA      50       33
> 
> The ideal graph will show each of the three questions as a different
> color line, with lines connecting years spanned by NA values.  What's the
> easiest way to do this?

Most of this can be done easily by plotting a multiple time series:

R> efficacy.mts <- ts(as.matrix(efficacy.df)[,2:4], start=1952)
R> plot(efficacy.mts, plot.type="single", col=1:3)

I'm not sure what is the easiest way to deal with the NAs. I would
probably interpolate the ts before plotting...
Hope that helps
Z

> Thanks.
> 
> ----------------------------------------------------------------------
> Andrew J Perrin - andrew_perrin at unc.edu - http://www.unc.edu/~aperrin
>  Assistant Professor of Sociology, U of North Carolina, Chapel Hill
>       269 Hamilton Hall, CB#3210, Chapel Hill, NC 27599-3210 USA
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> 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
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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