foverlaps()
в data.table
- это еще один способ.
structure(
list(
id = c(
"id_1", "id_2", "id_3", "id_4", "id_5", "id_6", "id_7", "id_8", "id_9",
"id_10", "id_11", "id_12", "id_13", "id_14", "id_15", "id_16", "id_17"
),
time_start = structure(
c(
37176, 30846, 30972, 38432, 31016, 37846, 30588, 31303, 37312, 30849,
38466, 30683, 38412, 30643, 29865, 30056, 31727
),
class = c("hms", "difftime"), units = "secs"
),
time_sec = c(
3987L, 2720L, 2812L, 1729L, 1851L, 3484L, 1881L, 2295L, 3770L, 3350L,
3626L, 2525L, 3570L, 2795L, 3606L, 4495L, 2517L
)
),
.Names = c("id", "time_start", "time_sec"),
row.names = c(NA, -17L),
class = c("tbl_df", "tbl", "data.frame")
) -> xdf
library(hrbrthemes) # devtools::install_git("https://gitlab.com/hrbrmstr/hrbrthemes")
library(data.table)
library(ggplot2)
library(dplyr)
library(tidyr)
arrange(xdf, time_start) %>%
mutate(id = factor(id, levels=rev(id))) %>%
ggplot(aes(time_start, id)) +
geom_segment(
aes(xend=time_start+time_sec, yend=id),
size=1.5, color=ft_cols$slate
) +
labs(
x = NULL, y = NULL, title = "Login/Usage Overlap Overview"
) +
theme_ipsum_rc(grid="X")
# Get the range
rng <- as.numeric(round(range(xdf$time_start), -1))
# 5-minute intervals
data_frame(
start = seq(rng[1], rng[2], 300),
end = start+299
) -> ranges
# the function we need requires a temporary conversion to a data.table
ranges <- data.table(ranges)
setkey(ranges, start, end)
# create start/end for oritinal data
xdf$start <- as.numeric(xdf$time_start)
xdf$end <- xdf$time_start + as.numeric(xdf$time_sec)
xdf <- data.table(xdf)
foverlaps(xdf, ranges) %>% # the magic is in this function
as_data_frame() %>% # back to normal
count(start) %>%
complete(start = ranges$start, fill=list(n=0)) %>%
ggplot(aes(start, n)) +
geom_line() + # there are other ways to show this data, too
geom_label(aes(label=n)) +
labs(
x = NULL, y = "# Users concurrently logged-in",
title = "Active Users per-5-minute Interval"
) +
theme_ipsum_rc()