Adding average into a matplot?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Adding average into a matplot?

-Halcyon-
Hi all,


I have a matrix which is filled with simulation results for several years.
Example of an output (7 years, 4 simulations):

     [,1] [,2] [,3] [,4]
[1,]  500  500  500  500  
[2,]  516  519  509  508  
[3,]  559  573  556  566  
[4,]  613  650  611  633  
[5,]  676  714  667  716  
[6,]  751  808  742  809
[7,]  816  890  823  879  

My matplot gives me 4 lines, but I would like to add a line with the averages of each year for all simulations. Can anyone help me with this?

Thanks in advance!
Reply | Threaded
Open this post in threaded view
|

Re: Adding average into a matplot?

Mike Prager
-Halcyon- <[hidden email]> wrote:

 
> I have a matrix which is filled with simulation results for several years.
> Example of an output (7 years, 4 simulations):
>
[...]
 
> My matplot gives me 4 lines, but I would like to add a line with the
> averages of each year for all simulations. Can anyone help me with this?

Take a look at help pages of the following functions:  mean,
apply, abline.


--
Mike Prager, NOAA, Beaufort, NC
* Opinions expressed are personal and not represented otherwise.
* Any use of tradenames does not constitute a NOAA endorsement.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Adding average into a matplot?

Bill.Venables
In reply to this post by -Halcyon-
Dear Kingfisher,

Try this.

X <- as.matrix(read.table(textConnection("
 500  500  500  500
 516  519  509  508
 559  573  556  566
 613  650  611  633
 676  714  667  716
 751  808  742  809
 816  890  823  879")))
 
Year <- 2000 + 1:7

matplot(Year, X, type = "l", pch = 3, col = "grey")
lines(Year, rowMeans(X), col = "red", lwd = 2)

 


Bill Venables
CSIRO Laboratories
PO Box 120, Cleveland, 4163
AUSTRALIA
Office Phone (email preferred): +61 7 3826 7251
Fax (if absolutely necessary):  +61 7 3826 7304
Mobile:                         +61 4 8819 4402
Home Phone:                     +61 7 3286 7700
mailto:[hidden email]
http://www.cmis.csiro.au/bill.venables/ 

-----Original Message-----
From: [hidden email] [mailto:[hidden email]]
On Behalf Of -Halcyon-
Sent: Friday, 11 April 2008 1:06 AM
To: [hidden email]
Subject: [R] Adding average into a matplot?


Hi all,


I have a matrix which is filled with simulation results for several
years.
Example of an output (7 years, 4 simulations):

     [,1] [,2] [,3] [,4]
[1,]  500  500  500  500  
[2,]  516  519  509  508  
[3,]  559  573  556  566  
[4,]  613  650  611  633  
[5,]  676  714  667  716  
[6,]  751  808  742  809
[7,]  816  890  823  879  

My matplot gives me 4 lines, but I would like to add a line with the
averages of each year for all simulations. Can anyone help me with this?

Thanks in advance!

--
View this message in context:
http://www.nabble.com/Adding-average-into-a-matplot--tp16609064p16609064
.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Adding average into a matplot?

-Halcyon-
In reply to this post by -Halcyon-

Thanks all for your online help and emails. It works fine now!

H



-Halcyon- wrote
Hi all,


I have a matrix which is filled with simulation results for several years.
Example of an output (7 years, 4 simulations):

     [,1] [,2] [,3] [,4]
[1,]  500  500  500  500  
[2,]  516  519  509  508  
[3,]  559  573  556  566  
[4,]  613  650  611  633  
[5,]  676  714  667  716  
[6,]  751  808  742  809
[7,]  816  890  823  879  

My matplot gives me 4 lines, but I would like to add a line with the averages of each year for all simulations. Can anyone help me with this?

Thanks in advance!