a b/R/HighlightByNode.R
1
#' Highlight Nodes in a Phylogenetic Tree with Custom Fill Colors and Transparency
2
#'
3
#' This function adds highlights to specific nodes in a phylogenetic tree represented by a `ggtree` object.
4
#' Users can specify the nodes to highlight along with custom fill colors, transparency, and extension options.
5
#'
6
#' @importFrom ggtree geom_hilight
7
#' @param ggtree_object A `ggtree` object representing the phylogenetic tree.
8
#' @param nodes A character vector specifying the nodes to highlight.
9
#' @param fill_colors A character vector specifying the fill colors for the highlighted nodes.
10
#' @param alpha_values A numeric vector specifying the transparency values for the highlighted nodes (between 0 and 1).
11
#' @param extend_values A logical vector specifying whether to extend the highlight to the whole clade below each node.
12
#' @return A modified `ggtree` object with the specified nodes highlighted.
13
#' @export
14
#'
15
#' @examples
16
#' plot_file <- system.file("extdata", "tree_plot.rds", package = "TransProR")
17
#' p2_plot <- readRDS(plot_file)
18
#'
19
#' # Please replace the following vectors with your specific values
20
#' nodes <- c(117, 129, 125, 127, 119,
21
#'            123, 139, 166, 124, 131, 217) # x-values of the nodes you want to highlight
22
#' fill_colors <- c("#CD6600", "#CD6600", "#CD6600",
23
#'                  "#CD6600", "#009933", "#009933",
24
#'                  "#009933", "#009933", "#9B30FF",
25
#'                  "#9B30FF", "#9B30FF") # Fill colors
26
#' alpha_values <- c(0.3, 0.3, 0.3, 0.3, 0.2, 0.3,
27
#'                   0.3, 0.3, 0.3, 0.3, 0.3) # Transparency values
28
#' extend_values <- c(25, 24, 24, 25, 25, 25,
29
#'                    24, 24, 25, 24, 24) # Values for the 'extend' parameter
30
#'
31
#' p2 <- highlight_by_node(
32
#'   p2_plot,
33
#'   nodes,
34
#'   fill_colors,
35
#'   alpha_values,
36
#'   extend_values
37
#' )
38
highlight_by_node <- function(ggtree_object,
39
                              nodes,
40
                              fill_colors,
41
                              alpha_values,
42
                              extend_values) {
43
  # Ensure that the lengths of `nodes`, `fill_colors`, `alpha_values`, and `extend_values` are consistent
44
  if (!(length(nodes) == length(fill_colors) && length(nodes) == length(alpha_values) && length(nodes) == length(extend_values))) {
45
    stop("Length of nodes, fill_colors, alpha_values, and extend_values must be the same.")
46
  }
47
48
  if (!requireNamespace("systemfonts", quietly = TRUE)) {
49
    stop("ggplot2 is required to use the function. Please install it.", call. = FALSE)
50
  }
51
52
  # For each node, add the corresponding geom_hilight layer
53
  layers <- lapply(seq_along(nodes), function(i) {
54
    node <- nodes[i]
55
    fill_color <- fill_colors[i]
56
    alpha_val <- alpha_values[i]
57
    extend_val <- extend_values[i]
58
59
    # Create a geom_hilight layer
60
    ggtree::geom_hilight(node = node, fill = fill_color, alpha = alpha_val, extend = extend_val)
61
  })
62
63
  # Add all layers to the `ggtree` object
64
  for (layer in layers) {
65
    ggtree_object <- ggtree_object + layer
66
  }
67
68
  return(ggtree_object)
69
}