[R] Error Message using xyplot of lattice

arun smartpink111 at yahoo.com
Sun Apr 6 21:07:24 CEST 2014


HI,
Check the output of:
datM <- melt(DataSeries,id.var="Date")
head(datM,2)# 3 columns
# Date variable value 

#1 2001-01-01 A 4
#2 2001-01-02 A 1
datM1 <- setNames(datM,LETTERS[c(24,26,25)])
library(lattice)
xyplot(Y~as.numeric(X),data=datM1,groups=Z,type="l") 


A.K.

hi, thank you for the help. doing this looks good, however I tried to adapt to code in order to be able to plot more than just 2 time series and I again get some Errors. using this: DataSeriesM <- setNames(melt(DataSeries,id.var="Date"),LETTERS[c(24,18,19,20,21,22,23,26,25)]) where DataSeries has 5000 observations of 9 variables, I get the error message: "Error in setNames(melt(DataSeries, id.var = "Date"), LETTERS[c(24, 18,  : 'names' attribute [9] must be the same length as the vector [3]" If I run this: length(LETTERS[c(24,18,19,20,21,22,23,26,25)])
length(DataSeries) I get 9 for both. I don't really understand why it is not working, is there anything you see? Using your example, I tried this: set.seed(495)
DataSeries <- data.frame(Date=seq(as.Date("2001-01-01"),by="1 day", length.out=20), A= sample(10,20,replace=TRUE), B= rnorm(20),C= rnorm(20)-1) DataSeriesM <- setNames(melt(DataSeries,id.var="Date"),LETTERS[c(24,26,25,22)]) which dives me the same error message. How do I Need to adapt this part as well as the code below to plot more than 2 lines? # with legends
xyplot(Y~as.numeric(X),xlab="X",data=DataSeriesM,type="l",groups=Z,as.table=TRUE,main="Development over time",auto.key=list(x=.85, y=.95, border=TRUE,lines=TRUE)) 


On Sunday, April 6, 2014 2:14 AM, arun <smartpink111 at yahoo.com> wrote:
Hi,
You didn't provide a reproducible example.  If your dataset is something like this:
set.seed(495)
DataSeries <- data.frame(Date=seq(as.Date("2001-01-01"),by="1 day", length.out=20), A= sample(10,20,replace=TRUE), B= rnorm(20))


###Using your codes:

Matrix_New1 <- cbind(as.list(DataSeries[,"Date"]),as.list(DataSeries[,"A"]),colnames(DataSeries)[2])

Matrix_New2 <- cbind(as.list(DataSeries[,"Date"]),as.list(DataSeries[,"B"]),colnames(DataSeries)[3])

str(Matrix_New1)
#List of 60
str(Matrix_New2) 

#List of 60 

Matrix_Complete<- rbind(Matrix_New1,Matrix_New2)
str(Matrix_Complete)
#List of 120
Col_Names_MC<-c("X","Y", "Z")
colnames(Matrix_Complete)<-Col_Names_MC 

library(lattice) 

Plot_AB<-xyplot(Y~ X, data =Matrix_Complete, type =c("l"), groups=Z, as.table=TRUE, main="Development over time")
#Error in eval(expr, envir, enclos) : object 'Z' not found 

#You could try:
library(reshape2)
 DataSeriesM <- setNames(melt(DataSeries,id.var="Date"),LETTERS[c(24,26,25)])
xyplot(Y~X,data=DataSeriesM,type="l",groups=Z,as.table=TRUE,main="Development over time")
 #for a numeric X axis
xyplot(Y~as.numeric(X),data=DataSeriesM,type="l",groups=Z,as.table=TRUE,main="Development over time") 
# with legends
xyplot(Y~as.numeric(X),xlab="X",data=DataSeriesM,type="l",groups=Z,as.table=TRUE,main="Development over time",auto.key=list(x=.85, y=.95, border=TRUE,lines=TRUE)) 


Hope it helps.

A.K.


Hi everbody, I have the following error message occuring when I run my code:
"Error in eval(expr, envir, enclos) : object 'Series' not found" The code I am using is the following: Matrix_New1 <- cbind(as.list(DataSeries[,"Date"]),as.list(DataSeries[,"A"]),colnames(DataSeries)[2] )
Matrix_New2 <- cbind(as.list(DataSeries[,"Date"]),as.list(DataSeries[,"B"]),colnames(DataSeries)[3] ) #whereas first and second columns should be numbers, third column is "A" and "B" respectively Matrix_Complete<- rbind(Matrix_New1,Matrix_New2) Col_Names_MC<-c("X","Y", "Z")
colnames(Matrix_Complete)<-Col_Names_MC Plot_AB<-xyplot(Y~ X, data =Matrix_Complete, type =c("l"), groups=Z, as.table=TRUE, main="Development over time") print(Plot_AB) Can you explain me why this way of creating a plot is not working and what I Need to Change in order to be able to plot it? 





More information about the R-help mailing list