-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessAnimateR_vignette.Rmd
78 lines (57 loc) · 2.73 KB
/
ProcessAnimateR_vignette.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
---
title: "User defined bins for time to change token aesthetics"
author: "Dominic Rowney"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{User defined bins for time to change token aesthetics}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
It is possible to change the aesthetics of tokens based on the timestamp of the animation
For example, the number of days a 'patient' has been in the system
```{r echo = TRUE, message = FALSE}
# Libraries ---------------------------------------------------------------
library(dplyr) ##pipes
library(tidyr) ##tidy data, partcularly the crossing() function
library(lubridate) ##date time manipulation
library(bupaR) ##buisness process analytics
library(processanimateR) ##animates process
# Create performance time flags ------------------------------------------------
my_flags <- data.frame(value = c(0,2,4,8,16)) %>%
mutate(day = days(value)) #convert numeric value into days
```
This will change the colour of the token at 0, 2, 4, 8, and 16 days.
The 'crossing()' function joins the cases of 'patients' to 'my_flags' and creates all possible combinations.
```{r echo = TRUE, message = FALSE}
# Create timestamps of flags ----------------------------------------------
my_timeflags <- patients %>%
cases %>%
crossing(my_flags) %>% ##similar to a SQL outer join
mutate(time = start_timestamp + day) %>%
filter(time <= complete_timestamp) %>%
select("case" = patient,time,value) ##must be case, time, value
```
The data for the 'token_scale()' function must have the column headings 'case, time, value'.
Without the 'domain = my_flags$value' arguement the flags follow alphabetic order (e.g.
0, 16, 2, 4, 8) rather than the numeric order we wants. See [d3-legend](https://github.com/susielu/d3-legend) for further information.
```{r echo = TRUE, message = FALSE}
# Animate process ---------------------------------------------------------
patients %>%
animate_process(mode ="absolute",
jitter=10,
legend = "color",
mapping = token_aes(
color = token_scale(my_timeflags
, scale = "ordinal"
, domain = my_flags$value
, range = rev(RColorBrewer::brewer.pal(5,"Spectral"))
)))
```
The colors can be modified through the 'range' argument. In this case the scale is reversed with 'rev()' to go from blue to red. See 'RColorBrewer::brewer.pal.info' for all options.