[R] Automating Plot Commands using a Loop

Koraelus koraelus at yahoo.com
Tue Nov 10 21:06:31 CET 2009


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:
> 
> try this:
> 
> Data <- read.table(textConnection(" Alpha Beta Gamma Delta
> A  .1     .2       .3        .4
> B   .2    .3       .4        .5
> C   .8   .9       .43       .13
> D  .13   .34      .34      .3"), header=TRUE)
> closeAllConnections()
> 
> par(mfrow=c(2,2))
> for (i in colnames(Data)){
>     plot(Data[[i]],type="o",axes=F,xlab='', ylab='', ylim=c(0,1), main=i)
>     axis(1,at=1:4,lab=c("A","B","C","D"))
>     axis(2,at=seq(0, 1, .25)) # changed your axis to work
> }
> 
> 
> On Mon, Nov 9, 2009 at 3:38 PM, Koraelus <koraelus at yahoo.com> wrote:
>>
>> I have three matrices with the same row and column names, but different
>> data.
>>
>> e.g.
>>
>> Data
>>  Alpha Beta Gamma Delta
>> A  .1     .2       .3        .4
>> B   .2    .3       .4        .5
>> C   .8   .9       .43       .13
>> D  .13   .34      .34      .3
>>
>>
>> For each column, I would like to create a separate plot on a single
>> window.
>> I currently am using the cmd-line:
>>
>> windows()
>> par(mfrow=c(2,2))
>> plot(Data$Alpha,type="o",axes=F,ann=F,ylim=c(0,1))
>> axis(1,at=1:4,lab=c("A","B","C","D")
>> axis(2,at=.25*0:1)
>> plot(Data$Beta,type="o",axes=F,ann=F,ylim=c(0,1))
>> axis(1,at=1:4,lab=c("A","B","C","D")
>> axis(2,at=.25*0:1)
>> etc.
>>
>> I would like to automate this as much as possible. I tried to write a
>> function, but clearly it would involve some sort of loop... I don't know
>> how
>> to do anything like that.
>>
>> Thanks,
>> koraelus
>> --
>> View this message in context:
>> http://old.nabble.com/Automating-Plot-Commands-using-a-Loop-tp26273268p26273268.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> 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.
>>
> 
> 
> 
> -- 
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
> 
> What is the problem that you are trying to solve?
> 
> ______________________________________________
> 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.
> 
> 

-- 
View this message in context: http://old.nabble.com/Automating-Plot-Commands-using-a-Loop-tp26273268p26289078.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list