Greetings, I would like to plot three lines on the same figure, and I am lost. There is an answer to a similar thread… but I tried matplot and it is beyond me. An example of the data follows: Year EM IM BM 1983 9.1 16.8 -7.7 1984 12.0 18.0 -6.0 1985 13.6 19.1 -5.5 1986 12.4 17.3 -4.9 1987 14.6 20.3 -5.7 1988 20.6 23.3 -2.6 1989 25.0 27.2 -2.2 1990 28.4 30.2 -1.8 1991 33.3 31.2 2.1 1992 40.6 35.2 5.4 1993 41.6 39.9 1.7 1994 50.8 49.5 1.3 1995 46.3 61.7 -15.4 1996 56.8 73.0 -16.2 1997 71.4 85.9 -14.5 1998 79.0 94.7 -15.7 1999 87.0 109.7 -22.7 2000 111.7 135.9 -24.2 2001 101.5 131.4 -29.9 2002 97.5 134.6 -37.1 2003 97.4 138.1 -40.6 2004 110.8 155.8 -45.1 Thanks, Mihai
assuming that your matrix is named x, try matplot(x,type="l") Ales Ziberna P.S.: see "?matplot" for help to matplot. That it all it took me! ----- Original Message ----- From: "Mihai Nica" <m_nica at hotmail.com> To: <r-help at stat.math.ethz.ch> Sent: Wednesday, June 15, 2005 8:50 PM Subject: [R] Multiple line plots> Greetings, > > I would like to plot three lines on the same figure, and I am lost. There > is > an answer to a similar thread. but I tried matplot and it is beyond me. An > example of the data follows: > > Year EM IM BM > 1983 9.1 16.8 -7.7 > 1984 12.0 18.0 -6.0 > 1985 13.6 19.1 -5.5 > 1986 12.4 17.3 -4.9 > 1987 14.6 20.3 -5.7 > 1988 20.6 23.3 -2.6 > 1989 25.0 27.2 -2.2 > 1990 28.4 30.2 -1.8 > 1991 33.3 31.2 2.1 > 1992 40.6 35.2 5.4 > 1993 41.6 39.9 1.7 > 1994 50.8 49.5 1.3 > 1995 46.3 61.7 -15.4 > 1996 56.8 73.0 -16.2 > 1997 71.4 85.9 -14.5 > 1998 79.0 94.7 -15.7 > 1999 87.0 109.7 -22.7 > 2000 111.7 135.9 -24.2 > 2001 101.5 131.4 -29.9 > 2002 97.5 134.6 -37.1 > 2003 97.4 138.1 -40.6 > 2004 110.8 155.8 -45.1 > > Thanks, > > Mihai > >--------------------------------------------------------------------------------> ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html
On Wed, 2005-06-15 at 13:50 -0500, Mihai Nica wrote:> Greetings, > > I would like to plot three lines on the same figure, and I am lost. There is > an answer to a similar thread but I tried matplot and it is beyond me. An > example of the data follows: > > Year EM IM BM > 1983 9.1 16.8 -7.7 > 1984 12.0 18.0 -6.0 > 1985 13.6 19.1 -5.5 > 1986 12.4 17.3 -4.9 > 1987 14.6 20.3 -5.7 > 1988 20.6 23.3 -2.6 > 1989 25.0 27.2 -2.2 > 1990 28.4 30.2 -1.8 > 1991 33.3 31.2 2.1 > 1992 40.6 35.2 5.4 > 1993 41.6 39.9 1.7 > 1994 50.8 49.5 1.3 > 1995 46.3 61.7 -15.4 > 1996 56.8 73.0 -16.2 > 1997 71.4 85.9 -14.5 > 1998 79.0 94.7 -15.7 > 1999 87.0 109.7 -22.7 > 2000 111.7 135.9 -24.2 > 2001 101.5 131.4 -29.9 > 2002 97.5 134.6 -37.1 > 2003 97.4 138.1 -40.6 > 2004 110.8 155.8 -45.1 > > Thanks, > > Mihai# First argument is x axis vector # Second argument is the y value vectors matplot(df$Year, df[, -1], type = "l", xlab = "Year", ylab = "Some Unit of Measure") If you want a legend, you could use the following: legend(1985, 150, legend = colnames(df)[-1], lty = 1:3, col = 1:3) See ?legend as well. HTH, Marc Schwartz
Here is an example. It assumes the data is in a data frame, with all variables numeric. tst <- data.frame(yr=1981:1990,y1=rnorm(10), y2=5+rnorm(10), y3=rnorm(10)-3) plot(tst$yr, tst$y1, ylim=range(tst[,-1]),type='l') lines(tst$yr,tst$y2,col=2) lines(tst$yr,tst$y3,col=3) -Don At 1:50 PM -0500 6/15/05, Mihai Nica wrote:>Content-Type: text/plain; format=flowed >X-MIME-Autoconverted: from 8bit to >quoted-printable by smtp-4.llnl.gov id >j5FIxVe7010769 > >Greetings, > >I would like to plot three lines on the same >figure, and I am lost. There is an answer to a >similar threadbut I tried matplot and it is>beyond me. An example of the data follows: > >Year EM IM BM >1983 9.1 16.8 -7.7 >1984 12.0 18.0 -6.0 >1985 13.6 19.1 -5.5 >1986 12.4 17.3 -4.9 >1987 14.6 20.3 -5.7 >1988 20.6 23.3 -2.6 >1989 25.0 27.2 -2.2 >1990 28.4 30.2 -1.8 >1991 33.3 31.2 2.1 >1992 40.6 35.2 5.4 >1993 41.6 39.9 1.7 >1994 50.8 49.5 1.3 >1995 46.3 61.7 -15.4 >1996 56.8 73.0 -16.2 >1997 71.4 85.9 -14.5 >1998 79.0 94.7 -15.7 >1999 87.0 109.7 -22.7 >2000 111.7 135.9 -24.2 >2001 101.5 131.4 -29.9 >2002 97.5 134.6 -37.1 >2003 97.4 138.1 -40.6 >2004 110.8 155.8 -45.1 > >Thanks, > >Mihai > > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html-- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA
On 6/15/05, Mihai Nica <m_nica at hotmail.com> wrote:> Greetings, > > I would like to plot three lines on the same figure, and I am lost. There is > an answer to a similar thread? but I tried matplot and it is beyond me. An > example of the data follows: > > Year EM IM BM > 1983 9.1 16.8 -7.7 > 1984 12.0 18.0 -6.0 > 1985 13.6 19.1 -5.5 > 1986 12.4 17.3 -4.9 > 1987 14.6 20.3 -5.7 > 1988 20.6 23.3 -2.6 > 1989 25.0 27.2 -2.2 > 1990 28.4 30.2 -1.8 > 1991 33.3 31.2 2.1 > 1992 40.6 35.2 5.4 > 1993 41.6 39.9 1.7 > 1994 50.8 49.5 1.3 > 1995 46.3 61.7 -15.4 > 1996 56.8 73.0 -16.2 > 1997 71.4 85.9 -14.5 > 1998 79.0 94.7 -15.7 > 1999 87.0 109.7 -22.7 > 2000 111.7 135.9 -24.2 > 2001 101.5 131.4 -29.9 > 2002 97.5 134.6 -37.1 > 2003 97.4 138.1 -40.6 > 2004 110.8 155.8 -45.1 >Suppose the data frame is dd. Then: matplot(dd[,1], dd[,-1], type = "l") Another possibility is to notice that this is a regularly spaced time series and it could be useful to represent it as a ts class object for here and for other purposes: ddts <- ts(as.matrix(dd[,-1]), start = dd[1,1]) plot(ddts, plot.type = "single", col = 1:3)