How to create a time scatterplot with R?

前端 未结 2 621
轻奢々
轻奢々 2021-02-03 12:33

The data are a series of dates and times.

date time
2010-01-01 09:04:43
2010-01-01 10:53:59
2010-01-01 10:57:18
2010-01-01 10:59:30
2010-01-01 11:00:44
…
         


        
相关标签:
2条回答
  • 2021-02-03 13:14

    I'd start by reading about as.POSIXct, strptime, strftime, and difftime. These and related functions should allow you to extract the desired subsets of your data. The formatting is a little tricky, so play with the examples in the help files.
    And, once your dates are converted to a POSIX class, as.numeric() will convert them all to numeric values, hence easy to sort, plot, etc.

    Edit: Andre's suggestion to play w/ ggplot to simplify your axis specifications is a good one.

    0 讨论(0)
  • 2021-02-03 13:17

    The ggplot2 package handles dates and times quite easily.

    Create some date and time data:

    dates <- as.POSIXct(as.Date("2011/01/01") + sample(0:365, 100, replace=TRUE))
    times <- as.POSIXct(runif(100, 0, 24*60*60), origin="2011/01/01")
    
    df <- data.frame(
      dates = dates,
      times = times
    )
    

    Then get some ggplot2 magic. ggplot will automatically deal with dates, but to get the time axis formatted properly use scale_y_datetime():

    library(ggplot2)
    library(scales)
    ggplot(df, aes(x=dates, y=times)) + 
      geom_point() + 
      scale_y_datetime(breaks=date_breaks("4 hour"), labels=date_format("%H:%M")) + 
      theme(axis.text.x=element_text(angle=90))
    

    enter image description here


    Regarding the last part of your question, on grouping by week, etc: To achieve this you may have to pre-summarize the data into the buckets that you want. You can use possibly use plyr for this and then pass the resulting data to ggplot.

    0 讨论(0)
提交回复
热议问题