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

77 views Asked by At

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

1

There are 1 answers

0
Allan Cameron On BEST ANSWER

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