Search code examples
rplotgmisc

Transition State Diagram R


My data looks something like this:

enter image description here

Output of dput(sequence_data);

    structure(list(Obs = 1:13, Seq.1 = structure(c(1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L), .Label = c("a", "b", "c"
), class = "factor"), Seq.2 = structure(c(1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("c", "d"), class = "factor"), 
    Seq.3 = structure(c(1L, 1L, 1L, 2L, 1L, 1L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L), .Label = c("", "d", "e"), class = "factor"), 
    Seq.4 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
    1L, 1L, 2L), .Label = c("", "f"), class = "factor")), .Names = c("Obs", 
"Seq.1", "Seq.2", "Seq.3", "Seq.4"), class = "data.frame", row.names = c(NA, 
-13L))

I am trying to get a transition state diagram. Here is the code:

transitions <- table(sequence_data$Seq.1,sequence_data$Seq.2) %>%
getRefClass("Transition")$new(label=c("1st Iteration", "2nd Iteration"))
transitions$box_width = 0.25;
transitions$box_label_cex = 0.7;
transitions$arrow_type = "simple";
transitions$arrow_rez = 300;
table(sequence_data$Seq.2,sequence_data$Seq.3) %>% transitions$addTransitions(label = '3rd Iteration')
transitions$render()

and here is the output: enter image description here

Can the empty values be removed from te diagram so that it looks more cleaner? I tried to remove but table statements needs the values to be of the same length.

I am using GMISC package (library(Gmisc)) for the graph.

Thanks


Solution

  • This may be a little hacky but will get you there. Basically you manually set the transitions (transitions$transitions) to 0.

    transitions$transitions[[2]][1,1] = 0
    transitions$transitions[[2]][2,1] = 0
    transitions$render()
    

    Maybe this loop can change all values to 0 automatically (although I haven't checked for large data yet)

    for (level_n in 1:length(transitions$transitions)){
        x =  transitions$transitions[[level_n]]
        for (cols in 1:ncol(transitions$transitions[[level_n]])){            
            if (dimnames(x)[[2]][cols] == ""){
                transitions$transitions[[level_n]][,cols] = 0
            }
        }
    }
    

    enter image description here