-1

Possible Duplicate:
Generate multiple graphics from within an R function

I am using this ggplot function to plot multiple variables by using a list:

plotTimeSeries <- list(n25_30,n28_30,n29_30,n31_30,n32_30)


for (i in plotTimeSeries) {
  i$dt <- strptime(i$dt, "%Y-%m-%d %H:%M:%S")
  ggplot(i, aes(dt, ambtemp)) + geom_line() +
    scale_x_datetime(breaks = date_breaks("2 hour"),
                     labels=date_format("%H:%M")) + 
    labs(x="Time 00.00 ~ 24:00 (2007-09-30)",y="Ambient Temperature",
         title = (paste("Node",i)))
}

data sample:

    ambtemp                  dt
1     -1.64 2007-09-29 00:01:09
2     -1.76 2007-09-29 00:03:09
3     -1.83 2007-09-29 00:05:09
4     -1.86 2007-09-29 00:07:09
5     -1.94 2007-09-29 00:09:09
6     -1.87 2007-09-29 00:11:09
7     -1.87 2007-09-29 00:13:09
8     -1.80 2007-09-29 00:15:09
9     -1.64 2007-09-29 00:17:09
10    -1.60 2007-09-29 00:19:09
11    -1.90 2007-09-29 00:21:09

I do not know how should I call the function in r. Actually, no error happened by code. On the other hand no results appeared by ruining this code.

2
  • I am quite new in R, would u calrify the problem. Than I can look for the solution better. Commented Jan 9, 2013 at 21:19
  • You have been pointed to no fewer than three extremely clear explanations for what's going wrong. Each of them says exactly what I would write myself. Commented Jan 9, 2013 at 21:22

1 Answer 1

2

When you are using a for-loop you are 'sort of' in a function. I say 'sort of' because assignments will occur in the global environment (assuming the loop was launched from the console) but ggplot and lattice plotting will not occur unless wrapped in print or the equivalent plot. So do this (and also read the FAQ):

for (i in plotTimeSeries) {
  i$dt <- strptime(i$dt, "%Y-%m-%d %H:%M:%S")
  print(
  ggplot(i, aes(dt, ambtemp)) + geom_line() +
    scale_x_datetime(breaks = date_breaks("2 hour"),
                     labels=date_format("%H:%M")) + 
    labs(x="Time 00.00 ~ 24:00 (2007-09-30)",y="Ambient Temperature",
         title = (paste("Node",i)))
        )  # append most recent plot
    }

I risking duplicating the earlier post but thought it might still be useful to expand on what is happening. When you type at the console there is an implicit 'read-eval-print' loop that is executing as soon as an kbd-return is submitted. When you run code in a loop the 'print' portion of that process is not automatic and in the case of the two grid plotting paradigms they silently return their values. If not explicitly printed or assigned there will be no result that can be recovered or will be pushed to a graphics device. If there had been assignment to a name the plots could have either been printed or modified further:

plotlist <- list()
for (i in plotTimeSeries) {
  i$dt <- strptime(i$dt, "%Y-%m-%d %H:%M:%S")
  plotlist <- c(plotlist, 
  ggplot(i, aes(dt, ambtemp)) + geom_line() +
    scale_x_datetime(breaks = date_breaks("2 hour"),
                     labels=date_format("%H:%M")) + 
    labs(x="Time 00.00 ~ 24:00 (2007-09-30)",y="Ambient Temperature",
         title = (paste("Node",i)))
        )
               )
    }

lapply(plotlist, print)
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, the problem of calling the function is solved but sth happened interesting. All the data that are used in list are appeared in top part of the plots.
I doubt we will be able to solve that with only a "data sample". Please post a reproducible example. I'm sure you have been advised to do this before.
Ok, I solved the recent problem already. I have added another list for titles. plotTimeSeries <- list(n25_29,n28_29,n29_29,n31_29,n32_29) Mainnn <- c('Node 25','Node 28','Node 29','Node 31','Node 32') for (j in Mainn){ for (i in plotTimeSeries) { i$dt <- strptime(i$dt, "%Y-%m-%d %H:%M:%S") print( ggplot(i, aes(dt, ambtemp)) + geom_line() + scale_x_datetime(breaks = date_breaks("2 hour"), labels=date_format("%H:%M")) + labs(x="Time 00.00 ~ 24:00 (2007-09-29)",y="Ambient Temperature", title = (paste(j))) ) } }
Shall I edit your last code with mine? Maybe in future these could be helpful for someone.
I do not think you can edit my answers with rep of 52. You can post anything you want at the end of your own question, though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.