tomaschwutz
2008-Feb-14 14:55 UTC
[R] plot each column of a matrix or dataframe versus x in a single plot
How do a plot several columns of a matrix at once in a single plot versus a single x-variable? The default plot.matrix or plot.dataframe commands plot each column versus each other column in several sub-plots. I want to plot each column versus a single other vector (x) as several lines or points in one plot. I can do it by hand: get the range of all variables (i.e. columns of the matrix or dataframe), initiate an empty plot command, loop through all the columns and issue a lines or points command. I guess there is a predefined sophisticated function for that, however, I was not able to find it. Does someone know where I find such a function? Best regards tomaschwutz
Dimitris Rizopoulos
2008-Feb-14 15:12 UTC
[R] plot each column of a matrix or dataframe versus x in a singleplot
you could use the function matplot(), e.g.,
x <- 1:20
vals <- 2 + rep(1:5, each = 20) * rep(x, 5) + rnorm(100)
dim(vals) <- c(20, 5)
matplot(x, vals, type = "l", lty = 1)
I hope it helps.
Best,
Dimitris
----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm
----- Original Message -----
From: "tomaschwutz" <thomas.wutzler at web.de>
To: <r-help at r-project.org>
Sent: Thursday, February 14, 2008 3:55 PM
Subject: [R] plot each column of a matrix or dataframe versus x in a
singleplot
> How do a plot several columns of a matrix at once in a single plot
> versus a single x-variable?
>
> The default plot.matrix or plot.dataframe commands plot each column
> versus each other column in several sub-plots. I want to plot each
> column versus a single other vector (x) as several lines or points
> in
> one plot.
>
> I can do it by hand: get the range of all variables (i.e. columns of
> the matrix or dataframe), initiate an empty plot command, loop
> through
> all the columns and issue a lines or points command.
> I guess there is a predefined sophisticated function for that,
> however, I was not able to find it.
>
> Does someone know where I find such a function?
>
> Best regards
> tomaschwutz
>
> ______________________________________________
> 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.
>
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Jorge Iván Vélez
2008-Feb-14 15:17 UTC
[R] plot each column of a matrix or dataframe versus x in a single plot
Hi Thomas, Ckeck ?matplot. Here's a simple example: # Data set x=runif(100) # Axis x1=rnorm(100,25,2) # Variable x_i x2=rnorm(100,10,1) x3=rnorm(100,15,4) x4=rnorm(100,30,4) DATA=cbind(x,x1,x2,x3,x4) # Plot matplot(x,DATA[,2:5],col=1:5,pch=1:5) I hope this helps. Jorge On 2/14/08, tomaschwutz <thomas.wutzler@web.de> wrote:> > How do a plot several columns of a matrix at once in a single plot > versus a single x-variable? > > The default plot.matrix or plot.dataframe commands plot each column > versus each other column in several sub-plots. I want to plot each > column versus a single other vector (x) as several lines or points in > one plot. > > I can do it by hand: get the range of all variables (i.e. columns of > the matrix or dataframe), initiate an empty plot command, loop through > all the columns and issue a lines or points command. > I guess there is a predefined sophisticated function for that, > however, I was not able to find it. > > Does someone know where I find such a function? > > Best regards > tomaschwutz > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
tomaschwutz
2008-Feb-14 16:07 UTC
[R] plot each column of a matrix or dataframe versus x in a single plot
Thank you Dimitris, Jorge, and Mark (personal mail), for the hint to matplot and the examples. This was exactly what I was thinking of. Thomas
John Kane
2008-Feb-14 23:19 UTC
[R] plot each column of a matrix or dataframe versus x in a single plot
Perhaps matplot will do what you want? ?matplot aa <- matrix(1:25, nrow=5) matplot(aa) --- tomaschwutz <thomas.wutzler at web.de> wrote:> How do a plot several columns of a matrix at once in > a single plot > versus a single x-variable? > > The default plot.matrix or plot.dataframe commands > plot each column > versus each other column in several sub-plots. I > want to plot each > column versus a single other vector (x) as several > lines or points in > one plot. > > I can do it by hand: get the range of all variables > (i.e. columns of > the matrix or dataframe), initiate an empty plot > command, loop through > all the columns and issue a lines or points command. > I guess there is a predefined sophisticated function > for that, > however, I was not able to find it. > > Does someone know where I find such a function? > > Best regards > tomaschwutz > > ______________________________________________ > 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. >[[elided Yahoo spam]]