### R Package Setup Source: https://rdrr.io/bioc/ggtreeExtra/f/vignettes/ggtreeExtra.Rmd Loads necessary R packages for tree visualization and extensions. Ensure these packages are installed before running. ```r library(ggtreeExtra) library(ggstar) library(ggplot2) library(ggtree) library(treeio) library(ggnewscale) ``` -------------------------------- ### Load and Use ggtree Source: https://rdrr.io/bioc/ggtree Load the ggtree library after installation to start using its functions for tree visualization. Use help() to access documentation. ```R library(ggtree) help(ggtree) ``` -------------------------------- ### Install ggtree Package Source: https://rdrr.io/bioc/ggtree Install the ggtree package using BiocManager. Ensure BiocManager is installed first if you haven't already. ```R if (!requireNamespace("BiocManager", quietly = TRUE)) install.packages("BiocManager") BiocManager::install("ggtree") ``` -------------------------------- ### Install ggtreeExtra Source: https://rdrr.io/bioc/ggtreeExtra/f/inst/doc/ggtreeExtra.Rmd Install the ggtreeExtra package from GitHub for development or from BiocManager for the release version. Ensure 'remotes' and 'BiocManager' are installed if needed. ```R # for devel if(!requireNamespace("remotes", quietly=TRUE)){ install.packages("remotes") } remotes::install_github("YuLab-SMU/ggtreeExtra") # for release if (!requireNamespace("BiocManager", quietly=TRUE)) install.packages("BiocManager") ## BiocManager::install("ggtreeExtra") ``` -------------------------------- ### Install ggtreeExtra Package Source: https://rdrr.io/bioc/ggtreeExtra/f/vignettes/ggtreeExtra.Rmd Instructions for installing the ggtreeExtra package from GitHub (development version) or CRAN (release version). Includes necessary checks for remotes and BiocManager. ```R # for devel if(!requireNamespace("remotes", quietly=TRUE)){ install.packages("remotes") } remotes::install_github("YuLab-SMU/ggtreeExtra") # for release if (!requireNamespace("BiocManager", quietly=TRUE)) install.packages("BiocManager") ## BiocManager::install("ggtreeExtra") ``` -------------------------------- ### Install ggtreeExtra Package Source: https://rdrr.io/bioc/ggtreeExtra Install the latest version of the ggtreeExtra package using BiocManager. Ensure BiocManager is installed first if you haven't already. ```r if (!requireNamespace("BiocManager", quietly = TRUE)) install.packages("BiocManager") BiocManager::install("ggtreeExtra") ``` -------------------------------- ### Example: Display root edge with default settings Source: https://rdrr.io/bioc/ggtree/src/R/geom_rootedge.R Demonstrates how to display the root edge of a phylogenetic tree using geom_rootedge with default settings. This example assumes the tree object has a root.edge defined. ```R library(ggtree) set.seed(123) ## with root edge = 1 tree1 <- read.tree(text='((A:1,B:2):3,C:2):1;' ggtree(tree1) + geom_tiplab() + geom_rootedge() ``` -------------------------------- ### Install ggtreeExtra Source: https://rdrr.io/bioc/ggtreeExtra/src/inst/doc/ggtreeExtra.R Instructions for installing the ggtreeExtra package from GitHub (development version) or Bioconductor (release version). ```R # for devel if(!requireNamespace("remotes", quietly=TRUE)){ install.packages("remotes") } remotes::install_github("YuLab-SMU/ggtreeExtra") # for release if (!requireNamespace("BiocManager", quietly=TRUE)) install.packages("BiocManager") ## BiocManager::install("ggtreeExtra") ``` -------------------------------- ### Setup Data for Continuous Color Source: https://rdrr.io/bioc/ggtree/src/R/geom_tree.R Prepares data for continuous color mapping in tree visualizations. Handles interpolation for smooth color gradients. ```R setup_data_continuous_color <- function(x, xend, y, yend, col, col2, xrange = NULL, nsplit = 100, extend = 0.002) { if (is.null(xrange)) xrange <- c(x, xend) ## xstep <- diff(xrange)/nsplit ## xn <- floor((xend - x)/xstep) xn <- floor((xend - x) * nsplit /diff(xrange)) ## slope <- (yend - y)/(xend - x) ydiff <- yend - y xdiff <- xend - x if (xn > 0) { ## x <- x + 0:xn * xstep x <- x + 0:xn * diff(xrange) / nsplit tmp <- x[-1] * (1 + extend) tmp[tmp > xend] <- xend xend <- c(tmp, xend) ## y <- y + 0:xn * xstep * slope y <- y + 0:xn * diff(xrange) * ydiff / (nsplit * xdiff) ## yend <- y + (xend - x) * slope yend <- y + (xend - x) * ydiff / xdiff } n <- length(x) if (is.numeric(col) && is.numeric(col2)) { colour <- seq(col, col2, length.out = n) } else if (is.character(col) && is.character(col2)) { colour <- grDevices::colorRampPalette(c(col, col2))(n) } else { stop("col and col2 should be both numeric or character..." ) } data.frame(x = x, xend = xend, y = y, yend = yend, colour = colour) } ``` -------------------------------- ### Example Usage of fortify.phyloseq Source: https://rdrr.io/bioc/ggtree/src/R/method-fortify.R Demonstrates how to call the fortify method for a phyloseq object. This is typically used within an R environment to prepare data for ggtree plotting. ```R library(ggtree) help(fortify.phyloseq) ``` -------------------------------- ### Load ggtree package and view help Source: https://rdrr.io/bioc/ggtree/f/vignettes/ggtree.Rmd Loads the ggtree package into the R session and displays its help documentation. This is a common first step when starting to use the package. ```r library(ggtree) help(ggtree) ``` -------------------------------- ### Load and Get Help for ggtreeExtra in R Source: https://rdrr.io/bioc/ggtreeExtra/src/R/reexports.R Loads the ggtreeExtra library and displays its help documentation. This is useful for understanding the package's functions and usage. ```r library(ggtreeExtra) help(ggtreeExtra) ``` -------------------------------- ### Example Usage of check_reverse Source: https://rdrr.io/bioc/ggtreeExtra/src/R/method-ggplot-add.R Demonstrates how to load the ggtreeExtra package and access the help documentation for the 'check_reverse' function. This is a standard way to explore R package functions. ```R library(ggtreeExtra) help(check_reverse) ``` -------------------------------- ### Example Usage of get_clade_position_ Source: https://rdrr.io/bioc/ggtree/src/R/geom_hilight.R Demonstrates how to use the `get_clade_position_` function, which is likely an internal helper function for `StatHilight` to determine clade positions. This example is typically run interactively. ```R library(ggtree) help(get_clade_position_) ``` -------------------------------- ### Load ggtreeExtra Package Source: https://rdrr.io/bioc/ggtreeExtra/f/NEWS.md Loads the ggtreeExtra package and displays its help documentation. This is a common starting point for exploring the package's functionality. ```r library(ggtreeExtra) help(ggtreeExtra) ``` -------------------------------- ### Plotting Trees Colored by OTU Group Source: https://rdrr.io/bioc/ggtree/src/R/ggdensitree.R Reads example tree data, computes OTU groups, and plots multiple trees colored by these groups with specified alpha transparency. This example uses `groupOTU` to define groups and `scale_colour_manual` to set custom colors. ```R # Read example data trees <- read.tree(system.file("examples", "ggdensitree_example.tree", package="ggtree")) # Compute OTU gr <- list(A = c("a.t1", "a.t2", "a.t3", "a.t4"), B = c("b.t1", "b.t2", "b.t3", "b.t4"), C = c("c.t1", "c.t2", "c.t3", "c.t4")) trees <- lapply(trees, groupOTU, gr) # Plot multiple trees colored by OTU ggdensitree(trees, aes(colour=group), alpha=1/6) + scale_colour_manual(values=c("black", "red", "green", "blue")) ``` -------------------------------- ### Converting Tree Formats Source: https://rdrr.io/bioc/ggtree/f Provides examples of converting tree objects between different formats, such as from Newick to a format compatible with ggtree. ```R library(ggtree) newick_string <- "(t1:1,t2:1,(t3:1,t4:1):1);" tree <- read.tree(text=newick_string) ggtree(tree) ``` -------------------------------- ### Plotting Time-Aligned Trees with Tip Labels Source: https://rdrr.io/bioc/ggtree/man/ggdensitree.html Visualizes multiple time-calibrated trees with aligned tips and tip labels, using a specified tip order. Generates example time-calibrated trees. ```R # Generate example data set.seed(1) trees <- rmtree(5, 10) time.trees <- lapply(1:length(trees), function(i) { tree <- trees[[i]] tree$tip.label <- paste0("t", 1:10) dates <- estimate.dates(tree, 1:10, mu=1, nsteps=1) tree$edge.length <- dates[tree$edge[, 2]] - dates[tree$edge[, 1]] fortify(tree) %>% mutate(tree=factor(i, levels=as.character(1:10))) }) # Plot multiple trees with aligned tips from muliple time points ggdensitree(time.trees, aes(colour=tree), tip.order=paste0("t", 1:10)) + geom_tiplab(colour='black') ``` -------------------------------- ### Highlighting a Specific Node Source: https://rdrr.io/bioc/ggtree/man/geom-hilight.html This example demonstrates how to highlight a single node (node=62) with the default fill color and then highlight another node (node=88) with a specified fill color ('red'). ```R library(ggplot2) set.seed(102) tree <- rtree(60) p <- ggtree(tree) p1 <- p + geom_hilight(node=62) + geom_hilight(node=88, fill="red") p1 ``` -------------------------------- ### ggtree Package Setup Source: https://rdrr.io/bioc/ggtree/src/inst/doc/ggtree.R Sets up knitr options for tidy and message output. Defines helper functions for creating CRAN and Bioconductor package links. ```R knitr::opts_chunk$set(tidy = FALSE, message = FALSE) CRANpkg <- function (pkg) { cran <- "https://CRAN.R-project.org/package" fmt <- "[%s](%s=%s)" sprintf(fmt, pkg, cran, pkg) } Biocpkg <- function (pkg) { sprintf("[%s](http://bioconductor.org/packages/%s)", pkg, pkg) } ``` -------------------------------- ### Box Plot Dodging with position_dodgex2 Source: https://rdrr.io/bioc/ggtreeExtra/man/position_dodgex.html Demonstrates using `position_dodgex2` with box plots. This example shows basic dodging and dodging with vertical and horizontal expansion. ```R library(ggplot2) library(patchwork) iris$ID <- rep(c(rep("test1", 15), rep("test2", 15), rep("test3", 20)),3) p <- ggplot(iris, aes(x=Species,y=Petal.Length,fill=ID)) p5 <- p + geom_boxplot(position=position_dodgex2()) p6 <- p + geom_boxplot(position=position_dodgex2(vexpand=5)) p7 <- ggplot(iris, aes(x=Petal.Length, y=Species, fill=ID)) + geom_boxplot(orientation="y", position=position_dodgex2(hexpand=5)) p8 <- p5 + p6 + p7 p8 ``` -------------------------------- ### Setup Data for Continuous Color Tree Source: https://rdrr.io/bioc/ggtree/src/R/geom_tree.R Applies setup_data_continuous_color to a data frame for tree visualization, processing each row to generate interpolated data for coloring branches. ```R setup_data_continuous_color_tree <- function(df, nsplit = 100, extend = 0.002) { lapply(1:nrow(df), function(i) { df2 <- setup_data_continuous_color(x = df$x[i], xend = df$xend[i], y = df$y[i], yend = df$yend[i], col = df$col[i], col2 = df$col2[i], xrange = range(df$x), nsplit = nsplit, extend = extend) df2$node <- df$node[i] j <- match(c('x', 'xend', 'y', 'yend', 'col', 'col2', 'colour'), colnames(df)) merge(df[i, -j, drop = FALSE], df2, by = "node") }) %>% do.call('rbind', .) } ``` -------------------------------- ### Helper function for tree data setup Source: https://rdrr.io/bioc/ggtree/src/R/geom_tree.R Ensures that the tree data is in the correct format for processing by tree geoms. It handles cases where the input data might not be ordered by node. ```R setup_tree_data <- function(data) { if (nrow(data) == length(unique(data$node))) return(data) data[match(unique(data$node), data$node),] ## data[order(data$node, decreasing = FALSE), ] } ``` -------------------------------- ### Get Subtree Unrooted Example Source: https://rdrr.io/bioc/ggtree/man/getSubtreeUnrooted.df.html Demonstrates how to use the getSubtreeUnrooted.df function to extract subtrees from a tree data frame. This function returns a named list of subtrees, where each list name corresponds to the starting node ID of the subtree. ```r getSubtreeUnrooted.df(df, node) ``` -------------------------------- ### Get Subtree from Data Frame Tree Source: https://rdrr.io/bioc/ggtree/man/getSubtree.df.html Use this function to get all child node IDs from a specified starting node in a tree represented as a data frame. The search is performed using a breadth-first traversal. ```R getSubtree.df(df, node) ``` -------------------------------- ### Loading ggtreeExtra and accessing help Source: https://rdrr.io/bioc/ggtreeExtra/src/R/position_points_jitterx.R Demonstrates how to load the ggtreeExtra library and access the help documentation for the position_raincloudx function. ```R library(ggtreeExtra) help(position_raincloudx) ``` -------------------------------- ### layoutDaylight Algorithm Steps Source: https://rdrr.io/bioc/ggtree/man/layoutDaylight.html Outlines the initial steps of the Equal-daylight Algorithm for tree layout, including leaf identification and initialization using the equal angle algorithm. ```R Leafs are subtrees with no children Initialise tree using equal angle algorithm tree_df = equal_angle(tree) nodes = get list of nodes in tree_df breadth-first nodes = remove tip nodes. ``` -------------------------------- ### Get Subtree Nodes Source: https://rdrr.io/bioc/ggtree/man/getSubtree.html Use this function to get all child node IDs from a specified starting node in an ape phylo tree object. Ensure the node ID provided is valid within the tree structure. ```R getSubtree(tree, node) ``` -------------------------------- ### Visualize tree with annotation data and labels Source: https://rdrr.io/bioc/ggtree/man/attacher.html This example demonstrates reading a tree, creating annotation data, and then plotting the tree with annotations. It uses `ggtree` to initialize the plot, `%<+%` to add data, and `geom_text` to display labels colored by a categorical variable. Missing values in the annotation data will be removed. ```r nwk <- system.file("extdata", "sample.nwk", package="treeio") tree <- read.tree(nwk) p <- ggtree(tree) dd <- data.frame(taxa=LETTERS[1:13], place=c(rep("GZ", 5), rep("HK", 3), rep("CZ", 4), NA), value=round(abs(rnorm(13, mean=70, sd=10)), digits=1)) row.names(dd) <- NULL p %<+% dd + geom_text(aes(color=place, label=label), hjust=-0.5) ``` -------------------------------- ### R: Loading and accessing help for as.polytomy Source: https://rdrr.io/bioc/ggtree/src/R/as.polytomy.R Demonstrates how to load the ggtree library and access the help documentation for the as.polytomy function in R. ```R library(ggtree) help(as.polytomy) ``` -------------------------------- ### Get Subtree from Data Frame Source: https://rdrr.io/bioc/ggtree/src/R/tree-utilities.R Extracts all child nodes from a tree represented as a data frame, starting from a given node. Uses tidytree's offspring function for data frames. ```R getSubtree.df <- function(df, node){ offspring.tbl_tree(df, node, self_include = TRUE)$node } ``` -------------------------------- ### Get Subtree from Ape Phylo Object Source: https://rdrr.io/bioc/ggtree/src/R/tree-utilities.R Retrieves all child nodes of a specified node from an ape phylo tree object, including the starting node itself. Uses tidytree's offspring function. ```R getSubtree <- function(tree, node){ tidytree::offspring(tree, node, self_include = TRUE) } ``` -------------------------------- ### Get Taxa Names from a Tree Source: https://rdrr.io/bioc/ggtree/man/get_taxa_name.html This snippet demonstrates how to use the get_taxa_name function with a ggtree object to retrieve an ordered list of taxa names from the entire tree. Ensure the ggtree package is installed and loaded. ```R library(ggtree) tree <- rtree(30) p <- ggtree(tree) get_taxa_name(p) ``` -------------------------------- ### Plotting Trees with Aesthetic Mapping and Tip Labels Source: https://rdrr.io/bioc/ggtree/src/R/ggdensitree.R Shows how to plot multiple trees with aligned tips, where the color of the trees is mapped to a variable. This example fortifies the trees first, adds a 'tree' identifier, and then plots them with aesthetic mapping for color and black tip labels. ```R trees.fort <- list(trees[[1]] %>% fortify %>% mutate(tree="a"), trees[[2]] %>% fortify %>% mutate(tree="b")); ggdensitree(trees.fort, aes(colour=tree)) + geom_tiplab(colour='black') ``` -------------------------------- ### Get Nodes in Breadth-First Order Source: https://rdrr.io/bioc/ggtree/src/R/tree-utilities.R Traverses a tree structure (as a data frame) starting from the root and returns a list of node IDs in breadth-first order. Skips tip nodes during traversal. Requires 'tidytree' and 'treeio'. ```R getNodesBreadthFirst.df <- function(df){ root <- getRoot.df(df) if(treeio::isTip(df, root)){ return(root) } tree_size <- nrow(df) # initialise list of nodes res <- root i <- 1 while(length(res) < tree_size){ parent <- res[i] i <- i + 1 # Skip if parent is a tip. if(treeio::isTip(df, parent)){ next } # get children of current parent. children <- tidytree::child(df,parent)$node # add children to result res <- c(res, children) } return(res) } ``` -------------------------------- ### Load ggtreeExtra and Access Help Source: https://rdrr.io/bioc/ggtreeExtra/src/R/position_dodgex.r Loads the ggtreeExtra library and displays the help documentation for the get_outliers function. ```R library(ggtreeExtra) help(get_outliers) ``` -------------------------------- ### Get Nodes in Breadth-First Order using ggtree Source: https://rdrr.io/bioc/ggtree/man/getNodesBreadthFirst.df.html Use this function to get a list of node IDs from a tree data frame in breadth-first order. Ensure the input is a valid tree data frame. ```R getNodesBreadthFirst.df(df) ``` -------------------------------- ### Access ggtree Help Source: https://rdrr.io/bioc/ggtree/src/tests/testthat.R Load the ggtree library and access its help documentation. This is useful for understanding the package's functions and usage. ```r library(ggtree) help(ggtree) ``` -------------------------------- ### Load ggtree and Access groupOTU Help Source: https://rdrr.io/bioc/ggtree/src/R/method-groupOTU.R This R code loads the ggtree library and then accesses the help documentation for the groupOTU.ggtree method. This is useful for understanding how to use the function. ```R library(ggtree) help(groupOTU.ggtree) ``` -------------------------------- ### Example usage of facet_plot Source: https://rdrr.io/bioc/ggtree/src/R/facet_plot.R This example demonstrates how to use facet_plot to visualize trait data on a phylogenetic tree. It first creates a tree, then a data frame with tip labels and associated values, and finally plots the tree with the data using geom_point on a 'Trait' panel. ```R tr <- rtree(10) dd = data.frame(id=tr$tip.label, value=abs(rnorm(10))) p <- ggtree(tr) facet_plot(p, 'Trait', data = dd, geom=geom_point, mapping=aes(x=value)) ``` -------------------------------- ### getSubtree Source: https://rdrr.io/bioc/ggtree/man/getSubtree.html Retrieves all child nodes of a specified node in a tree, including the starting node itself. ```APIDOC ## getSubtree ### Description Get all children of node from tree, including start_node. ### Usage ``` getSubtree(tree, node) ``` ### Arguments #### Path Parameters - **tree** (ape phylo tree object) - The tree object to extract nodes from. - **node** (numeric) - The tree node id from which the tree is derived. ### Value list of all child node id's from starting node. ``` -------------------------------- ### Basic tip label addition with ggtree Source: https://rdrr.io/bioc/ggtree/man/geom_tiplab.html This example demonstrates how to add tip labels to a phylogenetic tree using the geom_tiplab function after creating a basic tree visualization with ggtree. Ensure the 'ape' and 'ggtree' packages are loaded. ```R require(ape) tr <- rtree(10) ggtree(tr) + geom_tiplab() ``` -------------------------------- ### decimal2Date Source: https://rdrr.io/bioc/ggtree/man/decimal2Date.html Converts a numerical representation of a date into a Date object. For example, it transforms '2014.34' into '2014-05-05'. ```APIDOC ## decimal2Date ### Description Converts a numerical representation of a date into a Date object. For example, it transforms '2014.34' into '2014-05-05'. ### Arguments * `x` (numerical number) - Required - The numerical number representing the date, e.g., 2014.34. ### Value Returns a Date object. ### Author(s) Guangchuang Yu ``` -------------------------------- ### get.path Function Source: https://rdrr.io/bioc/ggtree/man/get.path.html Calculates the path from a start node to an end node in a phylogenetic tree object. ```APIDOC ## get.path(phylo, from, to) ### Description Calculates the path from a start node to an end node in a phylogenetic tree. ### Parameters #### Path Parameters - **phylo** (phylo object) - Required - The phylogenetic tree object. - **from** (node) - Required - The starting node. - **to** (node) - Required - The ending node. ### Returns - node vector: A vector representing the path from the start node to the end node. ``` -------------------------------- ### Get Node Euclidean Distances Source: https://rdrr.io/bioc/ggtree/src/R/tree-utilities.R Calculates the Euclidean distances from a specified node to all other nodes in a tree data frame. ```R getNodeEuclDistances <- function(df, node){ # https://stackoverflow.com/questions/24746892/how-to-calculate-euclidian-distance-between-two-points-defined-by-matrix-contain#24747155 dist <- NULL for(i in 1:nrow(df)) dist[i] <- euc.dist(df[df$node==node, c('x', 'y')], df[i, c('x', 'y')]) return(dist) } ``` -------------------------------- ### ggtreeExtra Package Startup Message Source: https://rdrr.io/bioc/ggtreeExtra/src/R/zzz.R This function is called when the ggtreeExtra package is loaded. It displays the package version and citation information. ```R #' @importFrom utils packageDescription .onAttach <- function(libname, pkgname) { pkgVersion <- packageDescription(pkgname, fields="Version") msg <- paste0(pkgname, " v", pkgVersion, " ", "For help: https://yulab-smu.top/treedata-book/", "\n\n") citation <- paste0("If you use ", pkgname, " in published research, please cite the paper:\n\n", ggtreeExtra_citations(), "\n") packageStartupMessage(paste0(msg, citation)) } ``` -------------------------------- ### ggtree Help Documentation Source: https://rdrr.io/bioc/ggtree/src/R/ggplot-add-utilities.R This is a command to access the help documentation for the build_text_layer function within the ggtree package. ```R library(ggtree) help(build_text_layer) ``` -------------------------------- ### Get Trunk Path Source: https://rdrr.io/bioc/ggtree/src/R/tree-utilities.R Finds the path from the root to the node furthest from the root in a phylogenetic tree. Requires 'treeio' for tree manipulation. ```R get.trunk <- function(tr) { root <- getRoot(tr) path_length <- sapply(1:(root-1), function(x) get.path_length(tr, root, x)) i <- which.max(path_length) return(get.path(tr, root, i)) } ``` -------------------------------- ### Create Fan Layout Tree with ggtree Source: https://rdrr.io/bioc/ggtreeExtra/f/vignettes/ggtreeExtra.Rmd Initializes a phylogenetic tree with a fan layout using ggtree. Sets the open angle and line size. Displays the basic tree structure. ```R # We use ggtree to create fan layout tree. p <- ggtree(tree, layout="fan", open.angle=10, size=0.5) p ``` -------------------------------- ### Get Color Attribute from Plot Data Source: https://rdrr.io/bioc/ggtree/src/R/utilities.R Extracts the 'color' column from the data of a plot object. Assumes 'color' is a column in the data. ```R get_color_attribute <- function(p) { p$data[, "color"] } ``` -------------------------------- ### Basic ggtree plot with theme_tree2 Source: https://rdrr.io/bioc/ggtree/man/theme_tree2.html Demonstrates creating a basic phylogenetic tree plot using ggtree and applying the theme_tree2. Requires the ape and ggtree packages. ```R require(ape) tr <- rtree(10) ggtree(tr) + theme_tree2() ``` -------------------------------- ### Get Tree View from Plot Source: https://rdrr.io/bioc/ggtree/src/R/utilities.R Retrieves the current tree view from the plot environment. Use when the tree view is not explicitly passed. ```R get_tree_view <- function(tree_view) { if (is.null(tree_view)) tree_view <- last_plot() return(tree_view) } ``` -------------------------------- ### Get ggname from ggplot2 Namespace Source: https://rdrr.io/bioc/ggtree/src/R/geom_taxalink.R Imports the ggname function from the ggplot2 package's namespace, used for naming graphical objects. ```R ggname <- getFromNamespace("ggname", "ggplot2") ``` -------------------------------- ### Highlighting Nodes with Data Mapping Source: https://rdrr.io/bioc/ggtree/man/geom-hilight.html This example shows how to highlight nodes based on a data frame. The 'fill' aesthetic is mapped to the 'type' column in the 'dat' data frame, allowing for different fill colors for different types of highlighted clades. ```R dat <- data.frame(id=c(62, 88), type=c("A", "B")) p2 <- p + geom_hilight(data=dat, mapping=aes(node=id, fill=type)) p2 ``` -------------------------------- ### Basic Usage of position_identityx Source: https://rdrr.io/bioc/ggtreeExtra/man/position_identityx.html Demonstrates the default usage of position_identityx without any expansion, maintaining the original position. ```R library(ggplot2) library(patchwork) p <- ggplot(mtcars, aes(x=wt, y=mpg)) p1 <- p + geom_point(position=position_identityx()) + ylim(0, 50) ``` -------------------------------- ### Extracting tidytree Namespace Functions Source: https://rdrr.io/bioc/ggtree/src/R/tidytree.R These lines extract specific utility functions from the tidytree package namespace. Ensure the tidytree package is installed. ```R nodeid.tbl_tree <- utils::getFromNamespace("nodeid.tbl_tree", "tidytree") rootnode.tbl_tree <- utils::getFromNamespace("rootnode.tbl_tree", "tidytree") offspring.tbl_tree <- utils::getFromNamespace("offspring.tbl_tree", "tidytree") child.tbl_tree <- utils::getFromNamespace("child.tbl_tree", "tidytree") parent.tbl_tree <- utils::getFromNamespace("parent.tbl_tree", "tidytree") ``` -------------------------------- ### Combining Boxplot Dodging Examples Source: https://rdrr.io/bioc/ggtreeExtra/src/R/position_dodgex.r Combines multiple plots featuring dodged boxplots with different `position_dodgex2` configurations using `patchwork` for comparison. ```R p8 <- p5 + p6 + p7 p8 ``` -------------------------------- ### Plotting Multiple Trees with Aligned Tips Source: https://rdrr.io/bioc/ggtree/man/ggdensitree.html Demonstrates plotting multiple trees from a list of phylo objects with aligned tips and tip labels. Requires 'ape' and 'dplyr' packages. ```R require(ape) require(dplyr) # Plot mutliple trees with aligned tips trees <- list(read.tree(text="((a:1,b:1):1.5,c:2.5);"), read.tree(text="((a:1,c:1):1,b:2);")); ggdensitree(trees) + geom_tiplab() ``` -------------------------------- ### Bar Plot Dodging with position_dodgex Source: https://rdrr.io/bioc/ggtreeExtra/man/position_dodgex.html Demonstrates using `position_dodgex` with bar plots. This example shows basic dodging and dodging with vertical expansion. ```R library(ggplot2) library(patchwork) iris$ID <- rep(c(rep("test1", 15), rep("test2", 15), rep("test3", 20)),3) p <- ggplot(iris, aes(x=Species,y=Petal.Length,fill=ID)) p1 <- p + geom_bar(stat="identity",position=position_dodgex()) p2 <- p + geom_bar(stat="identity",position=position_dodgex(vexpand=5)) p3 <- ggplot(iris, aes(x=Petal.Length, y=Species, fill=ID)) + geom_bar(stat="identity", orientation="y", position=position_dodgex(hexpand=5)) p4 <- p1 + p2 + p3 p4 ``` -------------------------------- ### Adding Pie Charts to Nodes with nodepie Source: https://rdrr.io/bioc/ggtree/f Demonstrates how to add pie charts to tree nodes using `nodepie` to represent proportions or frequencies associated with each node. ```R library(ggtree) library(ape) tree <- rtree(5) data <- data.frame(node=c(1, 3, 5), A=c(10, 20, 30), B=c(5, 10, 15)) ggtree(tree) + nodepie(data, cols=c("A", "B")) ``` -------------------------------- ### Rectangular Tree Layout Source: https://rdrr.io/bioc/ggtree/man/tree-layout.html Use layout_rectangular() for a standard rectangular tree visualization. No specific setup is required beyond loading the ggtree package. ```R layout_rectangular() ```