Skip to content

Instantly share code, notes, and snippets.

@natbusa
Created October 14, 2013 15:05
Show Gist options
  • Save natbusa/6977135 to your computer and use it in GitHub Desktop.
Save natbusa/6977135 to your computer and use it in GitHub Desktop.
A gist about displaying POSIXlt intervals using R and ggplot
library("ggplot2")
#sample logfile
logfile = "
task a,2013-10-10 11:16:16,2013-10-10 11:21:16
task b,2013-10-10 11:15:16,2013-10-10 11:18:45
task c,2013-10-10 11:20:20,2013-10-10 11:21:00
task d,2013-10-10 11:14:23,2013-10-10 11:19:30
"
# read in the sample logfile in a data frame
df = read.csv(
text=logfile,
header=FALSE,
sep = ",",
stringsAsFactors=FALSE,
blank.lines.skip=TRUE
)
# give the column a proper name
names(df)=c('name', 'start','end')
#use strptime to convert time strings to POSIXlt
df$start = strptime(df$start, "%Y-%m-%d %H:%M:%OS")
df$end = strptime(df$end, "%Y-%m-%d %H:%M:%OS")
# Define the top and bottom of the errorbars
ggplot(df, aes(start, reorder(name, as.POSIXct(start)), colour = name)) +
geom_errorbarh(aes(xmin = start,xmax = end, height = 0.2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment