[R] plotting several columns of matrix in one graph

arun smartpink111 at yahoo.com
Sun Apr 6 03:15:26 CEST 2014


HI,

Your statement is not clear:
"that does not give me a vector with several elements but just one"

library(xts)
library(xtsExtra)
vec1 <- seq(as.Date("2002-01-01"), as.Date("2009-12-31"),by="1 day") 

length(vec1)
#[1] 2922 

head(vec1) 

#[1] "2002-01-01" "2002-01-02" "2002-01-03" "2002-01-04" "2002-01-05"
[6] "2002-01-06" 

set.seed(532)

mat1 <- matrix(cumsum(rnorm(length(vec1)*5)),ncol=5, dimnames=list(NULL,LETTERS[1:5]))
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)

#I am attaching the pdf generated by the above command.

A.K.





hi, thanks for your answer. when I run this: vec1 <- seq(as.Date("2002-01-01"), as.Date("2009-12-31"),by="1 day") that does not give me a vector with several elements but just one. I tried several of your proposed methods, but to be honest nothing really worked out as it should including titles, colour etc. Not I tried to use matplot as proposed but the plots I get are really strange, the code is the following: x <- Data_Prices[,"Date"]
y<- cbind(Data_Used[,"Column2"],Data_Used[,"Column3"],Data_Used[,"Column4"] ) matlines (x, y, type = "l", lty = 1:2, lwd = 1, pch = NULL, col = 1:2) In the end, I Need a plot with the Dates (x) on the xaxis and 3 lines In Addition, I would like to manipulate colours of the lines, legend etc. 


On Wednesday, April 2, 2014 10:55 PM, arun <smartpink111 at yahoo.com> wrote:
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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: plotseveralcolumns.pdf
Type: application/pdf
Size: 99164 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20140405/b9d7f2d2/attachment-0002.pdf>


More information about the R-help mailing list