---
title: "mtb: Time Related Plots"
author: "Y. Hsu"
date: '`r Sys.Date()`'
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{mtb: Time Related Plots}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup}
library(mtb)
```

## Background

It's common to have observations that were measured longitudinally.
Here are some functions that could present observations measured over time.


## How to use - plot event periods with grouped labels

This is a basic example which shows you how to 
plot intervals of events with group labels at the beginning of individual intervals.

```{r}
dt = data.frame( id=paste('ID', c(seq(1,5), seq(1,5)),sep=""), idn=c(seq(1,5),seq(1,5)), start=1800*seq(1,10)/3, end=1800*(seq(1,10)/3+seq(2,-2)), label=rep(c('A','B'),5) )
dt
```


```{r example_color_1, fig.width=7}
p=time_plot_interval( dt, xlab='Time', ylab='ID', legend_title='Group', arrow_wt=2, arrow_color='gray')
p
```

# How to use - create a static timeline plot

There is an existing function `vistime::gg_vistime()` that creates plots for time periods or single time points. There is also an article by Ben Alex Keen that uses `ggplot2` for plotting a series of events. 

The `time_plot_event()` function creates a simplified timeline plot.  

```{r}
dt = data.frame( id=c(rep('Sous Chef',3), rep('Both',3), rep('Chef',4)), idn=c(rep(1,3),rep(-1,3), rep(2,4)), start=1800*c(0,1,2, 0.5, 1.2, 3, 1,2,3,4), end=1800*c(3.5,NA,3, 2, 6, NA, 2,3.5,3, 3.5),
label=c('Turkey', 'Watch Temp', 'Gravy', 'Stuffing', 'Whipped Cream', 'Cookie', 'Cranberry', 'Potato', 'Green Bean', 'Pumpkin Pie'), labelend=c('', '', '~~?', '->>', 'not shown', '', '', '', '||', '->X'), color=c('Oven', 'Other', 'Stove','Oven','Other','Oven','Stove','Oven','Oven','Oven' ), type=c('b', 'p', 'i','i','p','p','p','b','i','i' ) )
dt[1:5,]
```

```{r, fig.width=7}
time_plot_event( dt ) 
```

Note that the `compact` option can reduce the use of vertical spaces.
However,  to avoid reversed interval like `event-10`, the input data set should have start time < end time.

```{r, fig.width=7}
time_plot_event( dt, compact=TRUE )
```
