[R] Data formatting for matplot

Peter Ehlers ehlers at ucalgary.ca
Mon Sep 28 13:44:56 CEST 2009


Tim,

If you really want to use matplot, it's not hard. You need to
feed it a matrix of x-values and a corresponding matrix of
y-values.

   id <- rep(1:5, len=100)
   x <- rnorm(100,5,1)
   y <- rnorm(100,20,5)

   xm <- matrix(x, ncol = 5, byrow = TRUE)
   ym <- matrix(y, ncol = 5, byrow = TRUE)
   matplot(x, y, pch = 1)

But surely you don't need matplot:

   plot(y ~ x, data = mydat, col = id)

-Peter Ehlers

Tim Clark wrote:
> Henrique,
> 
> Thanks for the suggestion.  I think I may not understand matplot() because the graph did not come out like it should have.  Gabor suggested:
> 
> library(lattice)
> xyplot(y ~ x, mydat, groups = id)
> 
> Which gave what I was looking for.  Is there a way to get matplot() to give the same graph?  I don't have to use matplot(), but would like to understand its use.
> 
> Thanks,
> 
> Tim
> 
> 
> Tim Clark
> Department of Zoology 
> University of Hawaii
> 
> 
> --- On Sun, 9/27/09, Henrique Dallazuanna <wwwhsd at gmail.com> wrote:
> 
>> From: Henrique Dallazuanna <wwwhsd at gmail.com>
>> Subject: Re: [R] Data formatting for matplot
>> To: "Tim Clark" <mudiver1200 at yahoo.com>
>> Cc: r-help at r-project.org
>> Date: Sunday, September 27, 2009, 4:47 PM
>> You can try this:
>>
>> matplot(do.call(cbind, split.dat))
>>
>> On Sun, Sep 27, 2009 at 11:42 PM, Tim Clark <mudiver1200 at yahoo.com>
>> wrote:
>>> Dear List,
>>>
>>> I am wanting to produce a multiple line plot, and know
>> I can do it with matplot but can't get my data in the format
>> I need.  I have a dataframe with three columns; individuals
>> ID, x, and y.  I have tried split() but it gives me a list
>> of matrices, which is closer but not quite what I need.
>>  For example:
>>> id<-rep(seq(1,5,1),length.out=100)
>>> x<-rnorm(100,5,1)
>>> y<-rnorm(100,20,5)
>>>
>>> mydat<-data.frame(id,x,y)
>>> split.dat<-split(mydat[,2:3],mydat[,1])
>>>
>>> I would appreciate your help in either how to get this
>> into a format acceptable to matplot or other options for
>> creating a multiple line plot.
>>> Thanks,
>>>
>>> Tim
>>>
>>>
>>>
>>> Tim Clark
>>> Department of Zoology
>>> University of Hawaii
>>>
>>> ______________________________________________
>>> 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.
>>
>>
>> -- 
>> Henrique Dallazuanna
>> Curitiba-Paraná-Brasil
>> 25° 25' 40" S 49° 16' 22" O
>>
> 
> 
> 
> 
> ______________________________________________
> 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.
> 
>




More information about the R-help mailing list