Skip to content
Prev 199800 / 398502 Next

Automating Plot Commands using a Loop

Hello,

Thank you very much. Your string makes perfect sense to me, but I get an
error when I try this:

Data<-read.csv("Datacull.txt",header=T,row.names=1)
TData<-t(Data)
PlotFunction<-function (x) {
par(mfrow=c(3,6))
for (i in colnames(x)) {
plot(x[[i]],type="o",axes=F,xlab='',ylab='',ylim=c(0,2),main=i)
axis(1,at=1:6,lab=c("A","B","C","D","E","F"))
axis(2,at=seq(0,2,.25))
}}
PlotFunction(TData)

I get an error:
Error in x[[i]] : subscript out of bounds

If I try to hard code it without using a function

for (i in colnames(TData)) {
plot(TData[[i]],type="o",axes=F,xlab='',ylab='',ylim=c(0,2),main=i)
axis(1,at=1:6,lab=c("A","B","C","D","E","F"))
axis(2,at=seq(0,2,.25))
}}

I get the similar error:
Error in TData[[i]] : subscript out of bounds

What does this mean?
jholtman wrote: