0

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.

2 Answers 2

2

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"
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, it works in the plot grids are in every 6 hours. Is it possible to make it shorter to every 3 or 4 hours?
1

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

2 Comments

Yes, you're right ! The help page says strptime converts character vectors to class "POSIXlt": its input ‘x’ is first converted by as.character. So your answer is better.
Identical, yes, but yours is cleaner, really :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.