0

What is the correct way to deal with datetimes in ggplot ?

I have data at several different dates and I would like to facet each date by the same time of day, e.g. between 1:30PM and 1:35PM, and plot the points between this time frame, how can I achieve this?

My data looks like:

datetime              col1

2015-01-02 00:00:01    20
...                    ...
2015-01-02 11:59:59    34
2015-02-19 00:00:03    12
...                    ...
2015-02-19 11:59:58    27

I find myself often wanting to ggplot time series using datetime objects as the x-axis but I don't know how to use times only when dates aren't of interest.

guy
  • 1,021
  • 2
  • 16
  • 40

2 Answers2

1

The lubridate package will do the trick. There are commands you could use, specifically floor_date or ceiling_date to transform your datetime array.

Joel Alcedo
  • 192
  • 4
0

I always use the chron package for times. It completely disregards dates and stores your time numerically (e.g. 1:30PM is stored as 13.5 because it's 13.5 hours into the day). That allows you to perform math on times, which is great for a lot of reasons, including calculating average time, the time between two points, etc.

For specific help with your plot you'll need to share a sample data frame in an easily copy-able format, and show the code you've tried so far.

This is a question I'd asked previously regarding the chron package, and it also gives an idea of how to share your data/ask a question that's easier for folks to reproduce and therefore answer: Clear labeling of times class data on horizontal barplot/geom_segment

jesstme
  • 604
  • 2
  • 10
  • 25
  • 1
    I've done previous work with chron package and you are right, it is pretty seamless when dealing with times only excluding dates. I just thought of it as a workaround than a longer term solution which i felt datetimes were meant for. – guy Aug 17 '17 at 14:53