user3334446
user3334446

Reputation: 45

Creating graphs on loop in r

I am new to r and hope to get some help with using a loop function to create graphs.

I hope to create 288 graphs, and the data are in stacked format. Each graph is created from 30 rows of data, so in total I have 288*30 = 8640 rows in my data.

I managed to create a first graph using this code-

 # setting range and table
 xrange <- range(0,300)
  yrange <- range(1,15)
  plot(xrange, yrange, type="n", xlab="Time in seconds", ylab="Performance")

# adding lines
lines(df$TimeStamp0[1:30],df$Pur[1:30], type="o", pch=4, col="red")
lines(df$TimeStamp0[1:30],df$Yel[1:30], type="o", pch=4, col="blue")
lines(df$PartTimeStamp0[1:30],df$PartPur[1:30], type="o", pch=20, col="green")
lines(df$PartTimeStamp0[1:30],df$PartYel[1:30], type="o", pch=20, col="orange")

There are four lines for four different performance components. I hope to create a loop to get this syntax to generate a graph every 30 rows through my entire dataset. I tried the "for loop" command (see below) but couldn't get it to work.

for(i in 1:288) {
   startRow=1, endRow=startRow+29
   #pasted the above graph creation syntax
   }

I would really appreciate any help, thanks!!

Upvotes: 0

Views: 859

Answers (1)

shirewoman2
shirewoman2

Reputation: 1950

Since you want a graph every 30 lines, you don't want to use the range 1:288; instead, you should use:

for(i in seq(1, 8640, by=30){

Also, you need to change the "1" in "startRow=1" to "i", i.e.

startRow=i
endRow=startRow+29

Upvotes: 2

Related Questions