-5

I wanted to create automatic plot function by using following code:

plotTimeSeries <- list(temp1,temp2)

lapply(1:length(plotTimeSeries) , function(i) 
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")) + xlab("Time 00.00 ~ 24:00 (2007-09-29)") + ylab("Ambient Tempreture")+
  opts(title = ("Node i")))

But I faced with the error below:

Error: unexpected ')' in:
"           scale_x_datetime(breaks = date_breaks("2 hour"),labels=date_format("%H:%M")) + xlab("Time 00.00 ~ 24:00 (2007-09-29)") + ylab("Ambient Tempreture")+
           opts(title = ("Node i")))"

Sample data properties:

'data.frame':   731 obs. of  2 variables:
 $ ambtemp: num  0.23 0.26 0.35 0.31 0.32 0.3 0.36 0.33 0.27 0.27 ...
 $ dt     : POSIXct, format: "2007-09-29 23:39:05" "2007-09-29 23:41:05" "2007-09-29 23:43:05" ...

Sample data:

temp1:

 surtemp    date_time
1    0.23 2007-09-29 23:39:05
2    0.26 2007-09-29 23:41:05
3    0.35 2007-09-29 23:43:05
4    0.31 2007-09-29 23:45:05
5    0.32 2007-09-29 23:47:05
6    0.30 2007-09-29 23:49:05

temp2:

     surtemp    date_time
1   -1.42 2007-09-28 23:39:09
2   -1.24 2007-09-28 23:41:09
3   -1.28 2007-09-28 23:43:09
4   -1.28 2007-09-28 23:45:09
5   -1.24 2007-09-28 23:47:09
6   -1.42 2007-09-28 23:49:09
6
  • No, I don not think problem cased because of that ')' since it goes back to the lapply(. If I remove the opts ')' the coresponding '(' should also be removed. After removing both I came up with other errors. Commented Jan 7, 2013 at 20:41
  • 1
    if you want both expressions (setting i$dt and running ggplot) to be executed in the body of the function, you need curly brackets {} around them Commented Jan 7, 2013 at 20:45
  • @BenBolker, I have tried that as: plotTimeSeries <- list(n25_30,n25_29) lapply{1:length(plotTimeSeries) , function(i) 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")) + xlab("Time 00.00 ~ 24:00 (2007-09-29)") + ylab("Ambient Tempreture")+ opts(title = ("Node i"))}. However, the problem still exist. Commented Jan 7, 2013 at 20:48
  • No, the curly brackets go around the body of the function. Iterative syntax debugging is frustrating. Can you please post a reproducible example tinyurl.com/reproducible-000 ? Commented Jan 7, 2013 at 20:51
  • unreproducible example is unreproducible. Try making it simpler and simpler and eventually you'll hit the one thing that is breaking it. Commented Jan 7, 2013 at 20:52

1 Answer 1

1

I think it would be less confusing to revert to a for loop. Try:

plotTimeSeries <- list(temp1,temp2,temp3,temp4)
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-29)",y="Ambient Temperature",
           title = (paste("Node",i)))
}
Sign up to request clarification or add additional context in comments.

7 Comments

Now, I faced with this error: 'opts' is deprecated. Use 'theme' instead. (Deprecated; last used in version 0.9.1) Setting the plot title with opts(title="...") is deprecated. Use labs(title="...") or ggtitle("...") instead. (Deprecated; last used in version 0.9.1) 'opts' is deprecated. Use 'theme' instead. (Deprecated; last used in version 0.9.1) Setting the plot title with opts(title="...") is deprecated. Use labs(title="...") or ggtitle("...") instead. (Deprecated; last used in version 0.9.1)
did you try switching from opts to theme as suggested by the error message???(!)??? (I edited my answer)
But by using theme I cannot utilize 'i'. I came up with a new error: Error in function (el, elname) : Element title must be a element_text object.
I'm sorry, someone else will have to help you. I can't keep doing this without a reproducible example. Good luck ...
@Topdombili: It's an R-FAQ that inside for-loops you need to print the ggplot object.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.