Skip to content

Automating Plot Commands using a Loop

5 messages · Koraelus, jim holtman

#
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
#
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:

  
    
#
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:

  
    
#
Why did you transpose the dataframe (TData <- t(Data))?  Is your data
in the same structure as is expected.  Provide either the data file
you are plotting, or at least an 'str' of the object.
On Tue, Nov 10, 2009 at 3:06 PM, Koraelus <koraelus at yahoo.com> wrote:

  
    
#
Hello,

Thank you for responding! The data is in the same format as the example I
showed earlier. The data is different from matrix to matrix, but the general
format is:

                  Time1    Time 2   Time3  Time4
Species1     1            3            4         5
Species2     3            4            5         6
Species3    3           4             5           6

The problem I had was that I wanted to graph the species values (which are
different from matrix to matrix) through time - i.e. values y-axis and time
x-axis, so I transposed the matrix such that the species would be the
columns and time would be the rows so that this would be easier to code. 

If I code it without using a formula:

plot(Data$Species1,type="o",axes=F,ann=F,ylim=c(0,1))

It works perfectly fine.

Thanks,
Andrew Z.


Why did you transpose the dataframe (TData <- t(Data))?  Is your data
in the same structure as is expected.  Provide either the data file
you are plotting, or at least an 'str' of the object.