绘制日期时间VS(其他变量温度,湿度,气压)

时间:2015-10-16 06:50:54

标签: r

        Date & Time Air-PSR   TEMP     HUM 
08/01/2014 00:13:00 961.471 31.282  63.008
08/01/2014 00:28:00 961.534 31.152  63.178
08/01/2014 00:43:00 961.559 31.036  63.336
08/01/2014 00:58:00 961.612 30.964  63.49
08/01/2014 01:13:00 961.46  30.883  63.642
08/01/2014 01:28:00 961.318 30.78   63.795
08/01/2014 01:43:00 961.39  30.697  63.943
08/01/2014 01:58:00 961.209 30.637  64.087
08/01/2014 02:13:00 961.172 30.578  64.23
08/01/2014 02:43:00 961.679 30.486  64.521
08/01/2014 02:58:00 961.342 30.32   64.695
08/01/2014 03:13:00 961.214 30.052  64.878
08/01/2014 03:28:00 961.5   29.839  65.056
08/01/2014 03:43:00 961.541 29.689  65.231
08/01/2014 03:58:00 961.796 29.576  65.405
08/01/2014 04:13:00 962.027 29.472  65.577
08/01/2014 04:28:00 962.469 29.407  65.748
08/01/2014 04:43:00 962.909 29.25   65.961
08/01/2014 04:58:00 962.313 29.004  66.171

我已完成以下绘制此数据集的内容:

temp <- read.csv("test_imp_DT.csv", stringsAsFactors = F)
temp
class(temp$DT) # it gives character
class(temp$RS) # it gives inteeger
as.POSIXct(strptime(temp$DT, "%Y-%m-%d %H:%M:%S")) # converting character to date and time

运行上面的代码后,我得到了NA形式的错误。所有数据都已转换为NA

替代方法:

temp[i]$DT = as.POSIXct(temp[i]$DT,format="%m/%d/%y %I:%M:%S")
plot(temp[i]$DT~temp[i]$RS,na.rm=TRUE),data=temp[i],type='l',col='red')

我收到此错误:

  

plot.window(...)出错:需要有限'ylim'值

当我改为y-limit时也会得到错误,即

plot(temp[i]$DT~temp[i]$RS,y = range(temp[i]$RS, na.rm=TRUE),data=temp[i],type='l',col='red')

以最简单的方式,我只想为上面给出的样本数据文件制作日期时间VS温度,日期时间VS湿度和日期时间VS气压的图表。

enter image description here

同样在第二阶段,我想每周制作单独的图表,因为我有4个月的数据集。

1 个答案:

答案 0 :(得分:0)

一种简单的方法是使用包lubridate。我看不到您数据的链接,因此我只会展示您感兴趣的转化示例:

library(lubridate)
dmy_hms("08/01/2014 00:13:00")
[1] "2014-01-08 00:13:00 UTC"

跟进您的问题,转换您应用的数据框中的变量 整个列的功能,例如:

x <- c(1,2,3)
y <- c("08/01/2014 00:13:00", "08/01/2015 00:13:00", "08/02/2015 00:13:00")
df <- data.frame(x,y, stringsAsFactors = F)
df
 x                   y
1 1 08/01/2014 00:13:00
2 2 08/01/2015 00:13:00
3 3 08/02/2015 00:13:00
class(df$y)
[1] "character"
df$y <- dmy_hms(df$y)
class(df$y)
[1] "POSIXct" "POSIXt"