The reprex below illustrates how to add variables to a dataframe that reformat presentation of the date time string, in one case, and convert the hour component to integer on the other. It also shows that ggplot can deal with datetime objects as an axis without the need to convert from datetime to an integer.
suppressPackageStartupMessages({
library(lubridate)
library(ggplot2)
})
DF <- structure(list(
Email = c(
"email1@gmail.com", "email2@gmail.com", "email2@gmail.com",
"email3@gmail.com", "email3@gmail.com"
), Join.Time = structure(c(
as.POSIXct("2020-12-09 13:04:00"),
as.POSIXct("2020-12-10 13:20:00"), as.POSIXct("2020-12-11 13:30:00"), as.POSIXct("2020-12-12 13:07:00"),
as.POSIXct("2020-12-09 13:29:00")
), class = c("POSIXct", "POSIXt"), tzone = "America/New_York"),
Leave.Time = structure(c(
as.POSIXct("2020-12-09 13:25:00"), as.POSIXct("2020-12-11 13:22:00"),
as.POSIXct("2020-12-10 14:01:00"), as.POSIXct("2020-12-12 13:29:00"), as.POSIXct("2020-12-09 14:33:00")
),
class = c("POSIXct", "POSIXt"), tzone = "America/New_York"
)
),
.Names = c("Email", "Join_Time", "Leave_Time"),
row.names = c(NA, -5L), class = "data.frame"
)
begin <- floor_date(min(DF[,2]) - hours(1), unit = "hour")
end <- floor_date(max(DF[,3]) + hours(1), unit = "hour")
DF
#> Email Join_Time Leave_Time
#> 1 email1@gmail.com 2020-12-09 16:04:00 2020-12-09 16:25:00
#> 2 email2@gmail.com 2020-12-10 16:20:00 2020-12-11 16:22:00
#> 3 email2@gmail.com 2020-12-11 16:30:00 2020-12-10 17:01:00
#> 4 email3@gmail.com 2020-12-12 16:07:00 2020-12-12 16:29:00
#> 5 email3@gmail.com 2020-12-09 16:29:00 2020-12-09 17:33:00
DF$Engaged <- DF$Leave_Time - DF$Join_Time
DF$Date_Only <- format_ISO8601(DF$Join_Time, usetz=FALSE, precision="ymd")
DF$Hour_Only <- hour(DF$Leave_Time)
DF
#> Email Join_Time Leave_Time Engaged
#> 1 email1@gmail.com 2020-12-09 16:04:00 2020-12-09 16:25:00 21 mins
#> 2 email2@gmail.com 2020-12-10 16:20:00 2020-12-11 16:22:00 1442 mins
#> 3 email2@gmail.com 2020-12-11 16:30:00 2020-12-10 17:01:00 -1409 mins
#> 4 email3@gmail.com 2020-12-12 16:07:00 2020-12-12 16:29:00 22 mins
#> 5 email3@gmail.com 2020-12-09 16:29:00 2020-12-09 17:33:00 64 mins
#> Date_Only Hour_Only
#> 1 2020-12-09 16
#> 2 2020-12-10 16
#> 3 2020-12-11 17
#> 4 2020-12-12 16
#> 5 2020-12-09 17
p <- ggplot(DF,aes(Date_Only,Engaged))
p + geom_point() +
theme_minimal()
#> Don't know how to automatically pick scale for object of type difftime. Defaulting to continuous.
