简体   繁体   中英

ggplot2 and strptime in R

I am using ggplot for ploting. I came up with a problem, the code is:

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

Data:

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

when I want to use strptime all the time_date column is changing to the "NA".

Plz guide me in this matter.

You can not use :%S because you don't have it in your data. Try this:

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"

You don't seem to have seconds in your timestamps. So maybe you should use :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM