R/formatTree.R

Defines functions .setColorTheme .setSizeTheme formatTree

Documented in formatTree

#' A theme function for formatting of an igraph to be shown in RedeR
#'
#' Applies formatting patterns to an igraph object 
#' according to predefined themes.
#' This formatting is used for plotting on the RedeR app interface.
#' 
#' @param gg An igraph object generated by either \code{\link{hclust2igraph}}
#' or \code{\link{phylo2igraph}}<igraph>.
#' @param theme An integer ranging from 1 to 5 with desired theme. \cr
#' Options are: \cr
#' 1- A clean black and blue theme, great for higher levels of user 
#' customization. \cr
#' 2- A theme with a palette of greens. \cr
#' 3- A theme with a palette of blues. \cr
#' 4- A theme with a palette of purples. \cr
#' 5- A theme with a palette of reds. \cr
#' For custom formatting, see \code{\link[RedeR]{addGraph}} for accepted 
#' parameters <integer>. \cr
#' @param cleanalias A logical that removes the node aliases when set to 
#' TRUE (default = FALSE) <logical>.
#' 
#' @return An igraph object with standard formatting for RedeR application.
#'
#' @seealso \code{\link[RedeR]{addGraph}}
#' @seealso \code{\link{treeAndLeaf}}
#'
#' @examples
#' hc <- hclust(dist(USArrests), "ave")
#' gg <- hclust2igraph(hc)
#' gg <- formatTree(gg = gg,
#'                  theme = 5)
#'          
#' @importFrom igraph E        
#' @export

formatTree <- function(gg, theme = 1, cleanalias = FALSE){
    #Checks
    tal.checks(name = "gg", para = gg)
    tal.checks(name = "theme", para = theme)
    tal.checks(name = "cleanalias", para = cleanalias)
    idx <- match(igraph::V(gg)$name, gg$intnodes)
    #Size determination based on number of leaves
    sz <- length(igraph::V(gg)$name) - length(gg$intnodes)
    if(sz <= 100){ 
        size <- "small"
    } else if(sz >100 && sz <= 250){
        size <- "medium"
    } else {
        size<-"large"
    }
    #Setting the node alias
    igraph::V(gg)$nodeAlias <- igraph::V(gg)$name
    igraph::V(gg)$nodeAlias[!is.na(idx)]<-""
    
    gg <- .setSizeTheme(gg, size, idx)
    gg <- .setColorTheme(gg, theme)
    
    #Cleaning the node aliases
    if (cleanalias == TRUE){
        igraph::V(gg)$nodeAlias <-""
    }
    return(gg)
}

.setSizeTheme <- function(gg, size, idx){
    #Setting the node font size
    switch (size,
            small = igraph::V(gg)$nodeFontSize <- 60,
            medium = igraph::V(gg)$nodeFontSize <- 100,
            large = igraph::V(gg)$nodeFontSize <- 200)
    #Setting the node size
    switch (size,
            small = igraph::V(gg)$nodeSize <- 120,
            medium = igraph::V(gg)$nodeSize <- 200,
            large = igraph::V(gg)$nodeSize <- 400)
    igraph::V(gg)$nodeSize[!is.na(idx)] <- 1
    #Setting the edge width
    switch (size,
            small = igraph::E(gg)$edgeWidth <- 13,
            medium = igraph::E(gg)$edgeWidth <- 30,
            large = igraph::E(gg)$edgeWidth <- 50)
    #nodeLineWidth
    switch (size,
            small = igraph::V(gg)$nodeLineWidth <- 5,
            medium = igraph::V(gg)$nodeLineWidth <- 10,
            large = igraph::V(gg)$nodeLineWidth <- 15)
    return(gg)
}
    
.setColorTheme <- function(gg, theme){
    #nodeLineColor
    switch (theme,
            igraph::V(gg)$nodeLineColor <- "#000000",
            igraph::V(gg)$nodeLineColor <- "#379683",
            igraph::V(gg)$nodeLineColor <- "#55CFD1",
            igraph::V(gg)$nodeLineColor <- "#7F61A5",
            igraph::V(gg)$nodeLineColor <- "#D4A59A")
    #Setting the node font color
    switch (theme,
            igraph::V(gg)$nodeFontColor <- "#000000",
            igraph::V(gg)$nodeFontColor <- "#05386B",
            igraph::V(gg)$nodeFontColor <- "#1F2833",
            igraph::V(gg)$nodeFontColor <- "#5C2018",
            igraph::V(gg)$nodeFontColor <- "#5C2018")
    #Setting the edge color
    switch (theme,
            igraph::E(gg)$edgeColor <- "#000000",
            igraph::E(gg)$edgeColor <- "#379683",
            igraph::E(gg)$edgeColor <- "#55CFD1",
            igraph::E(gg)$edgeColor <- "#7F61A5",
            igraph::E(gg)$edgeColor <- "#D4A59A")
    #Setting the node colors
    switch (theme,
            igraph::V(gg)$nodeColor <- "#190061",
            igraph::V(gg)$nodeColor <- "#5CDB95",
            igraph::V(gg)$nodeColor <- "#45A29E",
            igraph::V(gg)$nodeColor <- "#44318D",
            igraph::V(gg)$nodeColor <- "#BC4639")
    return(gg)
}

Try the TreeAndLeaf package in your browser

Any scripts or data that you put into this service are public.

TreeAndLeaf documentation built on Nov. 8, 2020, 4:51 p.m.