Skip to content
Prev 293523 / 398503 Next

Simple plot loop

On 05/04/2012 12:04 AM, Ben Neal wrote:
Hi Ben,
Okay, we're going to have to fake it.

Data2<-data.frame(MONTH=seq(0,10,by=2),
  TAG01=sample(200:400,6),TAG02=sample(200:400,6),
  TAG03=sample(200:400,6),TAG04=sample(200:400,6))
column_names<-names(Data2)
attach(Data2)
colorvec<-c("red","green","green","blue")
plot(MONTH,TAG01,ylim=c(200,400),type="o",col=colorvec[1])
for(column in 2:(length(Data2)-1))
  lines(MONTH,get(column_names[column+1]),
   type="o",col=colorvec[column])

I wasn't being careful enough. You have to use get() when passing the 
character string that is the name of the column. Always something else 
to learn.

Jim