g中的语法错误R中的函数 [英] Syntax error in ggplot Function in R

查看:102
本文介绍了g中的语法错误R中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  plotTimeSeries<  -  list(temp1,temp2)$我想创建一个自动绘图函数b 
$ b lapply(1:长度(plotTimeSeries),函数(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小时),labels = date_format(%H:%M) )+ xlab(Time 00.00〜24:00(2007-09-29))+ ylab(Ambient Tempreture)+
opts(title =(Node i)))

但我遇到以下错误:

 
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)))

p>

示例数据属性:

 'data.frame ':731 obs。 2个变量:
$ ambtemp:num 0.23 0.26 0.35 0.31 0.32 0.3 0.36 0.33 0.27 0.27 ...
$ dt:POSIXct,格式:2007-09-29 23:39:052007 -09-29 23:41:052007-09-29 23:43:05...

样本数据:

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



$ p $ > 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


解决方案

我认为它会少一些令人困惑的是为循环回复。尝试:
$ b $ pre $ plotTimeSeries< - 列表(temp1,temp2,temp3,temp4)
(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小时),
labels = date_format(%H:%M))+
labs(x =Time 00.00 〜24:00(2007-09-29),y =Ambient Temperature,
title =(paste(Node,i)))
}


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

解决方案

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)))
}

这篇关于g中的语法错误R中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆