Search code examples
rggplot2time-seriestidyverselubridate

Plotting Time Series Data in R and Tidy


I am trying to use lubridate to sort out time series data from my temperature sensors. I would ultimately like a plot that has time on the x axis and temperature on the y axis. I have been using the function parse_date_time to try and create a new date variable but all I get is NA.

temps<-temps %>% as_tibble() %>% 
  mutate(date = parse_date_time(Date.Time..GMT..0500, "mdYHM"))
temps

enter image description here


Solution

  • The problem is that you inserted a capital Y when the year part only contains two digits. So you should use a small-case y, i.e.

    temps %>% as_tibble() %>% 
      mutate(date = parse_date_time(Date.Time..GMT..0500, "mdyHM"))
    

    To produce a simple plot, here is a basic code

    ggplot(temps) +
      aes(x = date, y = TempF) +
      geom_line()
    

    For further details on the plot itself, I suggest you to have a look at ggplot2 documentation.


    In my sample data it worked

    temps <- data.frame(
      Date.Time..GMT..0500 = c("6/18/18 12:57", "6/18/18 13:57", "6/18/18 14:57"),
      var = c(1,2,3)
    )
    parse_date_time(temps$Date.Time..GMT..0500, "mdYHM")
    # [1] "2018-06-18 12:57:00 UTC" "2018-06-18 13:57:00 UTC" "2018-06-18 14:57:00 UTC"