How to plot two columns of "Time" that is Start_time and End_Time of a data frame in R -


i newbie r programming , trying learn basic plotting. have couple of questions

my data frame looks this

event_type   date           time_start    time_end event      9/18/2014      14:47:01      16:53:07 event b      9/18/2014      15:52:16      17:08:30 event      9/18/2014      16:26:19      16:53:58 event c      9/20/2014      19:25:51      19:25:51 event b      9/23/2014       3:40:39       4:38:07 event d      9/25/2014       8:15:40       8:59:40 

ques 1. want plot data 9/18/2014 9/23/2014. how select date range in r?

ques 2. want plot event_type on y-axis , date(or month column) on x-axis "time_start" , "time_end" plotted on graph can have @ comparison between events.

i searched kind of question did not got answer it.

any kind of appreciated. kindly me in learning r.

first convert start , end times date-times in r:

x <- transform(x,                start=as.posixct(paste(date, time_start), format='%m/%d/%y %h:%m:%s'),                end=as.posixct(paste(date, time_end), format='%m/%d/%y %h:%m:%s')) 

then use ggplot , draw rectangle each event on timeline:

  • x axis datetime
  • draw rectangle start end of each event
  • the reason custom y , yend 2 overlapping events of same type (e.g. 2 event on 9/18/2014) appear visually separate each other. (i imagine can similar geom_bar() , position="jitter" don't know how).
  • the reason ordering y group similar events on y axis, if don't want use 1:nrow(x) instead y.
  • the scale_y_discrete(breaks=null) suppress y numbers. customise them wish.

giving:

library(ggplot2) ggplot(transform(x, y=order(event_type, start)),        aes(x=start, xend=end, y=y, yend=y, col=event_type)) +   geom_segment(size=3) +   scale_y_discrete(breaks=null) 

enter image description here

as mentioned since x axis large (spans multiple days) , each event lasts 1-2 hours, can barely see bar each event. have think how wish sensibly display data, not point of question. example if @ first 3 events clearer:

x2 <- x[1:3,] ggplot(transform(x2, y=order(event_type, start)),        aes(x=start, xend=end, y=y, yend=y, col=event_type)) +   geom_segment(size=3) +   scale_y_discrete(breaks=null) 

enter image description here


Comments