【发布时间】:2013-01-07 20:32:18
【问题描述】:
我想使用以下代码创建自动绘图功能:
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")))
但我遇到了以下错误:
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")))"
示例数据属性:
'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" ...
样本数据:
温度1:
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
温度2:
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
【问题讨论】:
-
不,我不认为因为 ')' 而导致问题,因为它会回到 lapply(。如果我删除 opts ')',那么对应的 '(' 也应该被删除。之后删除两者我想出了其他错误。
-
如果你希望两个表达式(设置
i$dt和运行ggplot)都在函数体中执行,你需要在它们周围加上花括号{} -
@BenBolker,我试过这样:plotTimeSeries
-
不,大括号围绕着 函数 的主体。迭代语法调试令人沮丧。您能否发布一个可重现的示例 tinyurl.com/reproducible-000 ?
-
不可重现的例子是不可重现的。试着让它变得更简单更简单,最终你会遇到破坏它的一件事。
标签: r function plot ggplot2 syntax-error