[R] plotting several columns of matrix in one graph

arun smartpink111 at yahoo.com
Thu Apr 3 04:55:34 CEST 2014


HI,
It is better to show a reproducible example using ?dput().  May be this helps:
#vector

vec1 <- seq(as.Date("2002-01-01"), as.Date("2009-12-31"),by="1 day")
#Assuming that length of the vector is the same as ?nrow of matrix.

set.seed(532)
mat1 <- matrix(cumsum(rnorm(length(vec1)*5)),ncol=5, dimnames=list(NULL,LETTERS[1:5]))

library(xts) 

library(xtsExtra)
plot(as.xts(mat1,order.by=vec1),major.format="%Y",screens=1, main="Time series plot", auto.legend=TRUE,auto.grid=FALSE,col=5:1)

You should also check ?legend()

A.K.







thank you so much. it tried the 2nd way and it is working perfectly. could you also tell me how I can select a vector which is then used for the x axis? let's say I want some Dates 2002 to 2009, currently I have a vector called date which contains of every single day from 2002 to 2009, is there a way to get this on the x axis (just the year)? in Addition, is there some command to add a legend, titles, Change Color etc? From what I find on the Internet, I just don't understand how that is working. thanks again for the help! 


On Wednesday, April 2, 2014 6:41 PM, arun <smartpink111 at yahoo.com> wrote:
Hi,
May be this helps:
library(xts) 


library(xtsExtra)
data(sample_matrix)
plot(as.xts(sample_matrix),screens=1)
#or
library(zoo)
plot(as.zoo(sample_matrix), plot.type="single",col=1:ncol(sample_matrix))

You may also check ?matplot

A.K.


Hi everyone, I have started using R and although I am used to some other languages, I am struggling doing a plot that contains several lines which each correspond to a column of the Matrix which all my data. I tried to google it but unfortunately, it haven't found anything which helped me and also the description didn't really give me a hint what to do. Let's say I have Matrix calles Data_Set which consists of 6 columns and let's say 100 rows. in the first column, I have the date, which is also the x-axis of my plot. The next five column contain the time series, for each of them I want I line drawn in the plot. I have installed the lattice package and I tried several things using the xyplot command, but it didn't work. thanks so much for your help.




More information about the R-help mailing list