Skip to content
Prev 293429 / 398503 Next

Simple plot loop

Jim, thanks for the reply. I tried what you recommend, but I still get an error when running it, just as I did with the similar loops I was trying. Here is the error: 

Error in xy.coords(x, y) : 'x' and 'y' lengths differ

That comes from this code: 

#Get file
library(zoo)
setwd("/Users/benjaminneal/Documents/All Panama/1110_Panama/CNAT_Segmenting")
Data = read.csv("120503_CNAT_Summary.csv", header=T)

#fill in missing data from last observation
Data2 <- na.locf(Data)
attach(Data2)

#Plot line, and make it loop
for(column in names(Data2)[2:length(Data2)])
  lines(MONTH,column,type="o",pch=22,lty=2,col="blue")
------------------------------------------------
The problem perhaps is in my data. My columns are observations over time, column headers are individual names, and the first column is the time series in months. An example is here: 
MONTH   Tag101
0             234
2             236
4             239
8             300
10           320

This then repeats for different individuals . . . I think my problem must be that my length of MONTH is different than my length of observations of each column . . . except that it is not, as far as I can tell! Thank you for assisting me with this simple but frustrating problem - I have the greatest respect for those of you who provide these answers that so many of us read and utilize. Thank you. Ben Neal


-----Original Message-----
From: r-help-bounces at r-project.org on behalf of Jim Lemon
Sent: Thu 5/3/2012 3:40 AM
To: Ben Neal
Cc: r-help at r-project.org
Subject: Re: [R] Simple plot loop
On 05/03/2012 05:50 PM, Ben Neal wrote:
Hi Ben,
I think what you may want in your loop is this:

for(column in names(Data2)[2:length(Data2)])
  lines(MONTH,column,type="o",pch=22,lty=2,col="blue")

But, if you want the first two lines to be green, you'll probably have 
to get a vector of colors:

colorvec<-rep("blue",length(Data2))
colorvec[1]<-"red"
colorvec[2:3]<-"green"

and change the above to:

columnnames<-names(Data2)
for(column in 2:length(Data2))
  lines(MONTH,columnnames[column],type="o",pch=22,lty=2,col=colvec[column])

Jim

______________________________________________
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.