0

I have a time series data and I 'want to plot it by using this code:

temp  <- read.csv("C:/Thesis/Data_set/grosseroor/25.csv",
                        header = FALSE)

names(temp) <- c("dt","ambtemp")
require(ggplot2)
library(scales)
temp$dt <-strptime(as.character(temp$dt), format = "%Y/%m/%d %H:%M")
ggplot(temp, aes(dt, ambtemp)) + geom_line() +
  scale_x_datetime(breaks = date_breaks("5 hour"),labels=date_format("%H:%M")) +
  xlab("Time 00.00 ~ 24:00 (2007-09-29)") + ylab("Tempreture")

Sample data:

date_time                
    10/20/2007 4:52 -6.14
    10/20/2007 4:54 -6.17
    10/20/2007 4:56 -6.09
    10/20/2007 5:00 -7.2
    10/20/2007 5:02 -6.65
    10/20/2007 5:04 -6.04
    10/20/2007 5:16 -6.26
    10/20/2007 5:20 -6.52
    10/20/2007 5:22 -6.4
    10/20/2007 5:24 -6.92
    10/20/2007 5:26 -7.04
    10/20/2007 5:28 -6.84
    10/20/2007 5:30 -6.16
    10/20/2007 5:32 -7.13
    10/20/2007 5:34 -7.43
    10/20/2007 5:36 -6.84
    10/20/2007 5:38 -7.82
    10/20/2007 5:40 -6.84
    10/20/2007 5:42 -6.84
    10/20/2007 5:44 -7.46
    10/20/2007 5:46 -7.74
    10/20/2007 5:48 -8.04

But when I ran the line temp$dt <-strptime((temp$dt), format = "%Y/%m/%d %H:%M"), all the date-time column changed to NA.

2
  • What is the output of dim(temp)? Commented Jan 28, 2013 at 15:07
  • @SvenHohenstein, dim(temp) [1] 1367 2 and 'data.frame': 1367 obs. of 2 variables: $ dt : POSIXlt, format: NA NA NA NA ... $ ambtemp: Factor w/ 332 levels "-10 2","-10.02",..: 71 74 67 158 116 62 83 105 94 137 ... Commented Jan 28, 2013 at 17:47

1 Answer 1

1

The problem is due to a wrong format string in strptime. You specified %m/%d/%Y, but the data is formatted in a different way: month/day/year. Hence, you have to change the command to:

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

As you will see, the order of the date elements then automatically appears as year - month - day: 2007-10-20.

There's another issue with the column ambtemp. As one can see in your comment, it's a factor. I assume it should be numeric. You can transform it the with the following command:

temp$ambtemp <- as.numeric(as.character(temp$ambtemp ))

Now, you could plot the data (I did not change the following commands):

require(ggplot2)
library(scales)

ggplot(temp, aes(dt, ambtemp)) + geom_line() +
  scale_x_datetime(breaks = date_breaks("5 hour"),labels=date_format("%H:%M")) + 
  xlab("Time 00.00 ~ 24:00 (2007-09-29)") + 
  ylab("Tempreture")

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

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.