rvtree

In vtree: How to sort each node in descending order vs. alphabetical


By now, if we do this:

library(vtree)

vtree(mtcars, "cyl am",
      sameline = TRUE, 
      follow=list(cyl="4"))

we get this: enter image description here

I would like to sort in descending order without manipulating the data. Is this possible?

Desired output:

enter image description here


Solution

  • I can't see a way of doing this in the vtree function, but you can swap the nodes in the output if that is any use to you?

    vt <- vtree(mtcars, "cyl am",
                sameline = TRUE, 
                follow = list(cyl = "4"))
    
    str2 <- str <- strsplit(vt$x$diagram, "\n")[[1]]
    
    node5 <- grep("^Node_5", str)
    node6 <- grep("^Node_6", str)
    
    str2[node5] <- sub("Node_6", "Node_5", str[node6])
    str2[node6] <- sub("Node_5", "Node_6", str[node5])
    
    vt$x$diagram <- paste(str2, collapse = "\n")
    
    vt
    

    enter image description here