Ggplot2 count on timeperiods

I was having an issue with ggplot2 and I did not find any question adressing the matter. I have entry rows (projects) with Initial starting time and Ending time. I want to plot the count at every moment of the projects that are active (inside the period). So for example if the 16/03/22 a project is started the line would step up 1 unit, but if the next day one of the active projects finishes, the line would step down 1 unit. Thank you

Project Start End
Chelsea Stadium 2021-03-22 16:27 2021-03-28 19:20
Clippers Stadium 2021-03-21 19:23 2021-03-27 15:23
Heat Stadium 2021-03-25 11:29 2021-03-29 16:21

enter image description here

Upvotes: 0

Views: 47

Answers (1)

Jon Spring
Jon Spring

Reputation: 66415

library(tidyverse); library(lubridate)
my_data <- data.frame(
  stringsAsFactors = FALSE,
            Project = c("Chelsea Stadium", "Clippers Stadium", "Heat Stadium"),
             Start = c("2021-03-22 16:27",
                       "2021-03-21 19:23","2021-03-25 11:29"),
               End = c("2021-03-28 19:20",
                       "2021-03-27 15:23","2021-03-29 16:21")
) %>%
  mutate(across(Start:End, ~lubridate::ymd_hm(.x)))

My approach here is to convert the data in "longer" form, so that each start and end becomes a row which either adds or subtracts 1 from the cumulative count.

my_data %>%
  pivot_longer(-Project, names_to = "status", values_to = "date") %>%
  mutate(chg = if_else(status == "Start", 1, -1)) %>%
  add_row(chg = 0, date = min(my_data$Start) - 1) %>%  # to get starting line from zero
  arrange(date) %>%
  mutate(count = cumsum(chg)) %>%
  ggplot(aes(date, count)) +
  geom_step()

enter image description here

Upvotes: 1

Related Questions