Skip to content
Prev 387456 / 398502 Next

How to plot dates

On 3/16/2021 3:32 PM, Gregory Coats via R-help wrote:
I am only guessing about what you are looking for by way of a visual 
comparison.? Here is one way of visualizing the variation of event times

library (ggplot2)
myDat <- read.table(text =
"datetime
2021-03-12 05:16:46
2021-03-12 09:17:02
2021-03-12 13:31:43
2021-03-12 22:00:32
2021-03-13 09:21:43
2021-03-13 13:51:12
2021-03-13 18:03:13
2021-03-13 22:20:28
2021-03-14 08:59:03
2021-03-14 13:15:56
2021-03-14 17:25:23
2021-03-14 21:36:26",
sep = ",", header = TRUE)

# create datepart variable - dte
myDat$dte <- as.Date(myDat$datetime, "%m/%d/%Y", tz='')

# create timepart variable - tme
library(lubridate)
myDat$tme <- hms::hms(as.numeric(myDat$datetime - 
floor_date(myDat$datetime, "1 day"), unit="secs"))

# plot event date by time grouped by date
ggplot(myDat, aes(x=tme, y = dte, group = dte)) + geom_line() + 
geom_point() +
?????? expand_limits(y=as.Date(c('20210301', '20210331'),'%Y%m%d'))

If this doesn't help get you started, you need to provide a description 
of what you want your plot to look like.

Dan