I'd like to create a bar chart showing elapsed time of a series of events. Time displayed in the Y axis and Date in the X axis. I'd like y axis limits range from 6 am day 1 to 6 am day 2.
My data looks something like:
TS<- data.frame(Date = c("01/07/1985", "02/07/1985", "03/07/1985",
"04/07/1985", "05/07/1985", "06/07/1985", "07/07/1985", "08/07/1985",
"09/07/1985", "10/07/1985", "11/07/1985", "12/07/1985", "13/07/1985",
"14/07/1985", "15/07/1985", "16/07/1985", "17/07/1985", "18/07/1985",
"19/07/1985", "20/07/1985", "21/07/1985"), Event1_start = c("05:46",
"05:45", "05:45", "05:44", "05:44", "05:43", "05:43", "05:42",
"05:47", "05:47", "05:46", "05:46", "05:45", "05:45", "05:44",
"05:44", "05:43", "05:43", "05:42", "05:47", "05:47"), Event1_end = c("18:33",
"18:33", "18:33", "18:33", "18:33", "18:33", "18:32", "18:32",
"18:33", "18:33", "18:33", "18:33", "18:33", "18:33", "18:33",
"18:33", "18:33", "18:32", "18:32", "18:33", "18:33"), Event2_start = c("19:08",
"19:59", "20:52", "21:49", "22:48", "23:50", "00:52", "04:39",
"05:39", "06:39", "07:39", "08:39", "09:39", "10:39", "11:39",
"12:39", "13:39", "14:39", "15:39", "16:39", "17:29"), Event2_end = c("07:27",
"08:12", "09:00", "09:52", "10:49", "11:49", "12:51", "17:17",
"18:17", "19:17", "20:17", "21:17", "22:17", "23:17", "00:17",
"01:17", "02:17", "03:17", "04:17", "05:17", "06:00"), Event3_start = c("18:33",
"18:33", "18:33", "18:33", "18:33", "18:33", "18:32", "18:32",
"18:33", "18:33", "18:33", "18:33", "18:33", "18:33", "18:33",
"18:33", "18:33", "18:33", "18:33", "18:33", "18:33"), Event3_end = c("19:08",
"19:59", "20:52", "21:49", "22:48", "23:50", "00:52", "01:52",
"05:47", "05:46", "05:46", "05:45", "05:45", "05:44", "05:44",
"05:43", "05:43", "05:42", "05:42", "05:42", "05:42"))
I have been trying to use the gather function and geom_bar but I don't seem to get the code to work. I'd like to create a stacked bar chart showing the duration of each event over a 24 hour period,spanning between 6am day 1 to 6 am day 2. So, as result I would get 3 different segments corresponding to the total time of each event on each date.
Some of my code
L1<- TS %>%
gather(variable, value, 2:7) %>% mutate (variable = factor(variable, levels = c("Event1_start","Event1_end","Event2_start","Event2_end","Event3_start","Event3_end"))) %>%
ggplot(aes(x = Date, y = value, fill = variable)) + scale_y_continuous(breaks = c(6,12,18,0,6)) +
geom_bar(stat = "identity", width = 0.95) + scale_fill_manual(values = c("#c7e6ff","black","#f5eea6"), labels = c("Event1","Event2", "Event3"))
Any help would be greatly appreciated!
Thanks.