rcolorsvtree

Change gradient with customise colors


I am struggling to figure how to customise colors. Here is an example :

vtree(mtcars, "cyl am",rootfillcolor="yellow")

How to customise colors of "cyl" and am.

I want to cyl : 4="blue", 6="green",8="yellow"

and to am 0="pink" and 1="brown"


Solution

  • You could specify the sorted colors:

    library(vtree)
    vtree(mtcars, "cyl am", rootfillcolor = "yellow",
          specfill = list(
              cyl = c("blue", "green", "yellow"),
              am = c("pink", "brown")
              )
         )
    

    enter image description here

    Or if you need a proper mapping, you could use something like this:

    helper <- function(x, colmap) {
     if (!all(x %in% names(colmap))) {
       stop("you did not map all values to colors")
     }
      colmap[as.character(sort(unique(x)))]
    }
    
    amcol <- c("0" = "pink", "1" = "brown")
    amcol2 <- c("1" = "brown", "0" = "pink")
    cylcol <- c("4" = "blue", "6" = "green", "8" = "yellow") 
    
    vtree(mtcars, "cyl am", rootfillcolor = "yellow", 
          specfill = list(cyl = helper(mtcars$cyl, cylcol),
                          am = helper(mtcars$am, amcol)))
    
    vtree(mtcars, "cyl am", rootfillcolor = "yellow", 
          specfill = list(cyl = helper(mtcars$cyl, cylcol), 
                          am = helper(mtcars$am, amcol2)))
    

    Of course this could be further improved so that mtcars$cyl and mtcars$am are automatically derived.