The Transition-class is the advanced version of the transitionPlot that aims at illustrating transitions between classes over time. The original intent was to show how self-administered the Charnley classification behaves before and after surgery. The plot is a fancier version than what can be achieved using packages such as diagram, although it lacks some of the flexibility - most notable is the lack of ability to “go back in time”. Thus the current function only allows to show transitions from one state to the next and there is no going back to previous columns.
We will start by simulating some data similar to my article. Each observation has a sex and a Charnley class (A, B, or C). The transition is then dependent on both the sex and the Charnley class.
library(magrittr)
set.seed(1)
<- 100
n <-
data data.frame(
Sex = sample(c("Male", "Female"),
size = n,
replace = TRUE,
prob = c(.4, .6)),
Charnley_class = sample(c("A", "B", "C"),
size = n,
replace = TRUE))
<- function(Chrnl_name){
getProbs <- data.frame(
prob A = 1/3 +
$Sex == "Male") * .25 +
(data$Sex != "Male") * -.25 +
(data%in% "B") * -.5 +
(data[[Chrnl_name]] %in% "C") * -2 ,
(data[[Chrnl_name]] B = 1/3 +
$Sex == "Male") * .1 +
(data$Sex != "Male") * -.05 +
(data== "C") * -2,
(data[[Chrnl_name]] C = 1/3 +
$Sex == "Male") * -.25 +
(data$Sex != "Male") * .25)
(data
# Remove negative probabilities
t(apply(prob, 1, function(x) {
if (any(x < 0)){
<- x - min(x) + .05
x
}
x
}))
}
<- c("Charnley_class")
Ch_classes %<>% c(sprintf("%s_%dyr", Ch_classes, c(1,2,6)))
Ch_classes for (i in 1:length(Ch_classes)){
if (i == 1)
next;
<-
data[[Ch_classes[i]]] apply(getProbs(Ch_classes[i-1]), 1, function(p)
sample(c("A", "B", "C"),
size = 1,
prob = p)) %>%
factor(levels = c("A", "B", "C"))
}
The most simple use is to just supply the output from the table()
call:
library(Gmisc)
<- table(data$Charnley_class, data$Charnley_class_1yr) %>%
transitions getRefClass("Transition")$new(label = c("Before surgery", "1 year after"))
$render() transitions
Adding customizations are rather straight forward by setting the different field arguments:
<- table(data$Charnley_class, data$Charnley_class_1yr) %>%
transitions getRefClass("Transition")$new(label = c("Before surgery", "1 year after"),
# Control the box text via grid::gpar()
txt_gpar = grid::gpar(fontfamily = "serif"))
$title <- "Charnley class in relation to THR"
transitions$arrow_type <- "simple"
transitions$box_label_pos <- "bottom"
transitions$render() transitions
Underlying proportions can be visualized by splitting the box colors and blending the colors in the gradient arrows. The color blend is explained in the colorbar element at the bottom:
<- table(data$Charnley_class, data$Charnley_class_1yr, data$Sex) %>%
transitions getRefClass("Transition")$new(label = c("Before surgery", "1 year after"))
$title <- "Charnley class in relation to THR"
transitions$clr_bar <- "bottom"
transitions$render() transitions
The major advantage with the Transition-class is that it allows for transitions at multiple time points. We do this by calling the addTransitions function as shown below:
<- table(data$Charnley_class, data$Charnley_class_1yr, data$Sex) %>%
transitions getRefClass("Transition")$new(label = c("Before surgery", "1 year after"))
$title <- "Charnley class in relation to THR"
transitions$arrow_type <- "simple"
transitionstable(data$Charnley_class_1yr, data$Charnley_class_2yr, data$Sex) %>%
$addTransitions(label = "2 years after")
transitionslibrary(grid)
$max_lwd <- unit(.05, "npc")
transitions$render() transitions