2013-01-24 45 views
0

我使用ggplot进行绘图。 我想出了一个问题,代码:ggplot2和strptime在R

require(ggplot2) 
require(reshape2) 

df <- data.frame(HMn25_30$avg,for30$cgsbi_1,dt_hmn$dt) 
names(df)[1]='WSN 25' 
names(df)[2]='MetoSwiss Forecast' 
df.m <- melt(df, names(df)[3], names(df)[1:2]) 
df.m$dt_hmn.dt <- strptime(as.character(df.m$dt_hmn.dt), format = "%m/%d/%Y %H:%M:%S") 
p <- ggplot(df.m, aes(x = dt_hmn.dt, y = value, group = variable, color = variable)) 
size=14),axis.text.y = element_text(angle=00, vjust=0.5, size=30)) 
p 

数据:

> head(df.m) 
     dt_hmn.dt variable value 
1 9/29/2007 23:00 WSN 25 0.280 
2 9/30/2007 0:00 WSN 25 0.208 
3 9/30/2007 1:00 WSN 25 -0.264 
4 9/30/2007 2:00 WSN 25 -0.480 
5 9/30/2007 3:00 WSN 25 -0.708 
6 9/30/2007 4:00 WSN 25 -0.714 

str(df.m) 
'data.frame': 50 obs. of 3 variables: 
$ dt_hmn.dt: Factor w/ 25 levels "9/29/2007 23:00",..: 1 2 3 14 19 20 21 22 23 24 ... 
$ variable : Factor w/ 2 levels "WSN 25","MetoSwiss Forecast": 1 1 1 1 1 1 1 1 1 1 ... 
$ value : num 0.28 0.208 -0.264 -0.48 -0.708 -0.714 -0.498 -0.216 0.126 0.574 ... 
时,我想用 strptime所有TIME_DATE栏更改为“NA”

Plz在这件事上指导我。

回答

2

您不能使用:%S,因为您的数据中没有它。试试这个:

strptime(dt$dt_hmn.dt, "%m/%d/%Y %H:%M") 

# [1] "2007-09-29 23:00:00" "2007-09-30 00:00:00" "2007-09-30 01:00:00" 
#  "2007-09-30 02:00:00" "2007-09-30 03:00:00" "2007-09-30 04:00:00" 
+0

是的,它在剧情网格是每6小时工作。每3或4小时能缩短一次吗? –

1

你的时间戳似乎没有秒。所以也许你应该使用:

df.m$dt_hmn.dt <- strptime(as.character(df.m$dt_hmn.dt), format = "%m/%d/%Y %H:%M") 
+1

是的,你是对的!帮助页面显示_strptime将字符向量转换为类“POSIXlt”:其输入“x”首先被as.character_转换。所以你的回答更好。 – juba

+0

完全相同,是的,但你的是更清洁,真的:) – juba