### Install EBImage dependencies on Ubuntu Source: https://github.com/yulab-smu/ggtree/wiki/ggtree-installation Install necessary development libraries for EBImage on Ubuntu using apt-get. ```bash sudo apt-get install libfftw3-3 libfftw3-dev libtiff5-dev ``` -------------------------------- ### Install EBImage dependencies on RedHat Source: https://github.com/yulab-smu/ggtree/wiki/ggtree-installation Install necessary development libraries for EBImage on RedHat-based systems using yum. ```bash sudo yum install libtiff-devel fftw-devel ``` -------------------------------- ### Install ggtree using BiocManager Source: https://github.com/yulab-smu/ggtree/wiki/ggtree-installation Use this R code to install the ggtree package. Ensure BiocManager is up-to-date by running biocLite("BiocUpgrade") if necessary. ```r ## try http if https:// URLs are not supported source("https://bioconductor.org/biocLite.R") # biocLite("BiocUpgrade") # you may need this biocLite("ggtree") ``` -------------------------------- ### phyloseq Example Visualization Source: https://github.com/yulab-smu/ggtree/wiki/reproduce-phyloseq-example-using-ggtree This snippet shows how to plot a phylogenetic tree with associated data using the phyloseq package. It requires the phyloseq package and the GlobalPatterns dataset. ```r require(phyloseq) data(GlobalPatterns) GP <- prune_taxa(taxa_sums(GlobalPatterns) > 0, GlobalPatterns) GP.chl <- subset_taxa(GP, Phylum=="Chlamydiae") plot_tree(GP.chl, color="SampleType", shape="Family", label.tips="Genus", size="Abundance") ``` -------------------------------- ### Reproducing ggphylo Example with ggtree and RAxML Data Source: https://github.com/yulab-smu/ggtree/wiki/reproduce-ggphylo-example-using-ggtree This function uses ggtree to plot phylogenetic trees with associated data, mimicking the ggphylo example. It utilizes the `%<+%` operator and a custom 'raxml' class to incorporate bootstrap and population size data. ```r draw_ggphylo_ggtree <- function(x, bootstraps, pop.sizes) { xx <- new("raxml", phylo=x, bootstrap=data.frame(node=1:getNodeNum(x), bootstrap=bootstraps, pop.size=pop.sizes) ) ggtree(xx, aes(color=bootstrap), ladderize=F, size=1.2) + geom_point(aes(size=pop.size), subset=.(bootstrap>50), color="black") + geom_text(aes(label=node), subset=.(!isTip), size=3, color="grey50", hjust=-.5) + geom_tiplab(color="black", hjust=-.5, size=3) + scale_color_gradient(limits=c(50, 100), low="red", high="black") + scale_size_continuous(limits=c(0, 100000), range=c(1, 5)) + theme_tree2(legend.position="right") } draw_ggphylo_ggtree(x, bootstraps, pop.sizes) ``` -------------------------------- ### Tree Plot with Tip Labels and Points Source: https://github.com/yulab-smu/ggtree/wiki/Why-ggtree-is-special? This example shows how to add tip labels and points to a phylogenetic tree plot using ggtree. It builds upon the basic tree plot by incorporating geom_point and geom_tiplab for enhanced visualization. ```R ggplot(tr, aes(x, y)) + geom_tree() + geom_point() + geom_tiplab() ``` -------------------------------- ### Define Function to Draw OutbreakTools Example with ggtree Source: https://github.com/yulab-smu/ggtree/wiki/reproduce-OutbreakTools-example-using-ggtree Defines a function that uses ggtree to visualize phylogenetic trees, incorporating metadata like location and date, similar to OutbreakTools. ```r library(ggtree) draw_OutbreakTools_ggtree <- function(x) { tree <- x@trees[[1]] g <- ggtree(tree, right=T, mrsd="2009-09-30", as.Date=TRUE) + theme_tree2() g <- g + theme(panel.grid.major = element_line(color="grey"), panel.grid.major.y=element_blank()) ## extract location info meta.df <- x@dna@meta meta.df <- data.frame(taxa=rownames(meta.df), meta.df) loc <- x@individuals loc <- data.frame(individualID=rownames(loc), loc) meta_loc <- merge(meta.df, loc, by="individualID") meta_loc <- meta_loc[,-1] ## attach additional information to tree view via %<+% operator ## that was created in ggtree g <- g %<+% meta_loc g + geom_tippoint(aes(color=location), size=3, alpha=.75) + theme(legend.position="right") + scale_color_brewer("location", palette="Spectral") } draw_OutbreakTools_ggtree(x) ``` -------------------------------- ### Benchmarking ggphylo vs ggtree Performance Source: https://github.com/yulab-smu/ggtree/wiki/reproduce-ggphylo-example-using-ggtree Compares the execution time of plotting phylogenetic trees with associated data using ggphylo and ggtree. Uses the microbenchmark package for accurate timing. ```r require(microbenchmark) bm <- microbenchmark( ggphylo = draw_ggphylo(x, bootstraps, pop.sizes), ggtree = draw_ggphylo_ggtree(x, bootstraps, pop.sizes), times=100L ) print(bm) ## Unit: milliseconds ## expr min lq mean median uq max ## ggphylo 1230.81977 1266.51639 1283.54395 1284.05280 1294.22355 1467.62436 ## ggtree 30.40235 31.70079 33.48648 32.67046 34.05156 42.88493 ## neval ## 100 ## 100 qplot(y=time, data=bm, color=expr) + scale_y_log10() + ggtitle("run time comparison") ``` -------------------------------- ### phyloseq: Plotting a Phylogenetic Tree Source: https://github.com/yulab-smu/ggtree/wiki/Why-ggtree-is-special? This snippet illustrates plotting a phylogenetic tree using the phyloseq package. It generates tree layout data, initializes a ggplot object with segments for edges and vertical lines, and conditionally adds tip labels. ```r treeSegs <- tree_layout(phy_tree(physeq), ladderize=ladderize) edgeMap = aes(x=xleft, xend=xright, y=y, yend=y) vertMap = aes(x=x, xend=x, y=vmin, yend=vmax) ## Initialize phylogenetic tree. ## Naked, lines-only, unannotated tree as first layers. Edge (horiz) first, then vertical. p = ggplot(data=treeSegs$edgeDT) + geom_segment(edgeMap) + geom_segment(vertMap, data=treeSegs$vertDT) if(!is.null(label.tips)){ ## `tiplabDT` has only one row per tip, the farthest horizontal ## adjusted position (one for each taxa) tiplabDT = dodgeDT tiplabDT[, xfartiplab:=max(xdodge), by=OTU] tiplabDT <- tiplabDT[h.adj.index==1, .SD, by=OTU] if(!is.null(color)){ if(color %in% sample_variables(physeq, errorIfNULL=FALSE)){ color <- NULL } } labelMap <- NULL if(justify=="jagged"){ labelMap <- aes_string(x="xfartiplab", y="y", label=label.tips, color=color) } else { labelMap <- aes_string(x="max(xfartiplab, na.rm=TRUE)", y="y", label=label.tips, color=color) } ## Add labels layer to plotting object. p <- p + geom_text(labelMap, tiplabDT, size=I(text.size), hjust=-0.1, na.rm=TRUE) } ``` -------------------------------- ### Visualize BEAST Output with ggtree Source: https://github.com/yulab-smu/ggtree/wiki/reproduce-figtree-example-using-ggtree This snippet visualizes a time-scaled tree from BEAST output using ggtree. It requires the ggplot2 and ggtree packages. The `read.beast` function parses the tree file, and `ggtree` displays it, setting the most recent sampling date and mapping color to tree height. Customizations include adding text labels, adjusting x-axis breaks, and modifying grid lines. ```r require(ggplot2) require(ggtree) x <- read.beast("/Applications/FigTree/influenza.tree") ggtree(x, right=TRUE, mrsd="2014-04-02", aes(color=height)) + theme_tree2() + geom_text(aes(x=max(x), label=label), size=1, hjust=-.3) + scale_x_continuous(breaks=c(1992, 1995, 1997, 2000, 2002, 2005), minor_breaks=seq(1992, 2005, 1)) + geom_segment(aes(xend=max(x)+.20, yend=y), linetype="dotted", size=.1) + theme(panel.grid.major = element_line(color="black", size=.2), panel.grid.minor = element_line(color="grey", size=.2), panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank()) ``` -------------------------------- ### Load OutbreakTools Data and Plot Tree Source: https://github.com/yulab-smu/ggtree/wiki/reproduce-OutbreakTools-example-using-ggtree Loads the FluH1N1pdm2009 dataset from OutbreakTools and generates a phylogenetic tree visualization with location-based tip coloring. ```r library(OutbreakTools) data(FluH1N1pdm2009) attach(FluH1N1pdm2009) x <- new("obkData", individuals = individuals, dna = FluH1N1pdm2009$dna, dna.individualID = samples$individualID, dna.date = samples$date, trees = FluH1N1pdm2009$trees) p <- plotggphy(x, ladderize = TRUE, branch.unit = "year", tip.color = "location", tip.size = 3, tip.alpha = 0.75) ``` -------------------------------- ### ggphylo: Data Preparation for Tree Plotting Source: https://github.com/yulab-smu/ggtree/wiki/Why-ggtree-is-special? This snippet shows how data for tree lines, nodes, and labels are subsetted from a layout data frame in ggphylo. It initializes a ggplot object with the lines data. ```r lines.df <- subset(layout.df, type=='line') nodes.df <- subset(layout.df, type=='node') labels.df <- subset(layout.df, type=='label') internal.labels.df <- subset(layout.df, type=='internal.label') q <- ggplot(lines.df) geom.fn <- switch(aes.type, line='geom_joinedsegment', node='geom_point', label='geom_text', internal.label='geom_text' ) q <- q + do.call(geom.fn, geom.list) ``` -------------------------------- ### Benchmark OutbreakTools vs ggtree Performance Source: https://github.com/yulab-smu/ggtree/wiki/reproduce-OutbreakTools-example-using-ggtree Compares the execution time of plotting a tree using OutbreakTools' plotggphy function versus ggtree's custom function. ```r require(microbenchmark) bm <- microbenchmark( OutbreakTools = plotggphy(x, ladderize = TRUE, branch.unit = "year", tip.color = "location", tip.size = 3, tip.alpha = 0.75), ggtree = draw_OutbreakTools_ggtree(x), times=100L ) print(bm) ## Unit: milliseconds ## expr min lq mean median uq max neval ## OutbreakTools 564.0195 585.7710 1225.8299 1767.713 1784.8457 1984.3352 100 ## ggtree 144.2246 151.7099 159.3318 155.200 160.9121 328.2016 100 qplot(y=time, data=bm, color=expr) + ggtitle("run time comparison") ``` -------------------------------- ### OutbreakTools: Plotting a Phylogenetic Tree Source: https://github.com/yulab-smu/ggtree/wiki/Why-ggtree-is-special? This code demonstrates how to plot a phylogenetic tree using OutbreakTools. It converts a phylo object to a ggphy format, separates edge, tip, and node data, and initializes a ggplot object with the edge data. Tip labels can be added conditionally. ```r ggphy <- phylo2ggphy(phylo, tip.dates = tip.dates, branch.unit = branch.unit) ##TODO: allow edge and node attributes and merge with df.edge and df.node df.tip <- ggphy[[1]] df.node <- ggphy[[2]] df.edge <- ggphy[[3]] p <- ggplot(data = df.edge) p <- p + geom_segment(data = df.edge, aes(x = x.beg, xend = x.end, y = y.beg, yend = y.end), lineend = "round") p <- p + scale_y_continuous("", breaks = NULL) if (show.tip.label) { p <- p + geom_text(data = df.tip, aes(x = x, y = y, label = label), hjust = 0, size = tip.label.size) } ``` -------------------------------- ### Benchmarking phyloseq vs ggtree Plotting Source: https://github.com/yulab-smu/ggtree/wiki/reproduce-phyloseq-example-using-ggtree This snippet benchmarks the performance of the original phyloseq plot_tree function against the custom ggtree-based function draw_phyloseq_ggtree. It uses the microbenchmark package for timing. ```r require(microbenchmark) bm <- microbenchmark( phyloseq = plot_tree(GP.chl, color="SampleType", shape="Family", label.tips="Genus", size="Abundance"), ggtree = draw_phyloseq_ggtree(GP.chl), times=100L ) print(bm) ``` -------------------------------- ### Plotting a List of Phylo Objects with ggphylo Source: https://github.com/yulab-smu/ggtree/wiki/reproduce-ggphylo-example-using-ggtree Use this snippet to plot multiple phylogenetic trees stored in a list using the ggphylo package. Requires the ape and ggphylo libraries. ```r require(ape) require(ggphylo) tree.list = rmtree(3, 20) ggphylo(tree.list) ``` -------------------------------- ### OutbreakTools: Conditional Tip Label Addition Source: https://github.com/yulab-smu/ggtree/wiki/Why-ggtree-is-special? This snippet shows a conditional addition of tip labels in OutbreakTools. It highlights that if tip labels are not initially shown, the data required for them might be discarded, making later addition impossible without re-rendering. ```r if (show.tip.label) { p <- p + geom_text(data = df.tip, aes(x = x, y = y, label = label), hjust = 0, size = tip.label.size) } ``` -------------------------------- ### Plotting Multiple Phylo Objects with ggtree Source: https://github.com/yulab-smu/ggtree/wiki/reproduce-ggphylo-example-using-ggtree Reproduce ggphylo's multi-tree plotting capability using ggtree. This snippet visualizes a list of trees, coloring them by ID and faceting by ID. ```r require(ggplot2) require(ggtree) ggtree(tree.list, ladderize=F, aes(color=.id)) + facet_wrap(~.id) + geom_point() + geom_text(subset=.(!isTip), aes(label=node), hjust=-.2, size=3) + geom_tiplab(size=4, hjust=-.2) ``` -------------------------------- ### Basic Tree Plot with ggtree Source: https://github.com/yulab-smu/ggtree/wiki/Why-ggtree-is-special? This snippet demonstrates the fundamental usage of ggtree for plotting a phylogenetic tree. It requires a tree object and uses geom_tree to visualize it within a ggplot framework. ```R tr <- rtree(30) ggplot(tr, aes(x, y)) + geom_tree() ``` -------------------------------- ### ggtree Function to Draw phyloseq Tree Source: https://github.com/yulab-smu/ggtree/wiki/reproduce-phyloseq-example-using-ggtree A custom R function using ggtree to visualize phyloseq data, allowing for detailed annotation and aesthetic mapping. Requires ggplot2 and ggtree packages. ```r require(ggplot2) require(ggtree) draw_phyloseq_ggtree <- function(phyloseq) { tree <- phyloseq@phy_tree p <- ggtree(tree, ladderize = F) p <- p + geom_text(subset=.(!isTip), aes(label=label), hjust=-.2, size=4) dd <- psmelt(phyloseq) dd <- dd[dd$Abundance > 0, ] data <- merge(p$data, dd, by.x="label", by.y="OTU") spacing <- 0.02 idx <- with(data, sapply(table(node)[unique(node)], function(i) 1:i)) %>% unlist hjust <- spacing * idx * max(data$x) data$xdodge <- data$x + hjust p <- p + geom_point(data=data, aes(x=xdodge, color=SampleType, shape=Family, size=Abundance), na.rm=T) + theme(legend.position="right") + scale_size_continuous(trans=log_trans(5)) d2 <- unique(data[, c("x", "y", "Genus")]) p + geom_text(data=d2, aes(label=Genus), hjust=-.3, na.rm=T, size=4) } draw_phyloseq_ggtree(GP.chl) ``` -------------------------------- ### Plotting Tree Data with ggphylo Source: https://github.com/yulab-smu/ggtree/wiki/reproduce-ggphylo-example-using-ggtree Visualize data such as bootstrap values and population sizes along a phylogenetic tree using ggphylo. This function customizes tree appearance based on these attributes. ```r n <- 40; x <- rtree(n); n.nodes <- length(nodes(x)) bootstraps <- 100 - rexp(n.nodes, rate=5) * 100 pop.sizes <- pmax(0, rnorm(n.nodes, mean=50000, sd=50000)) draw_ggphylo <- function(x, bootstraps, pop.sizes) { for (i in nodes(x)) { x <- tree.set.tag(x, i, 'bootstrap', bootstraps[i]) x <- tree.set.tag(x, i, 'pop.size', pop.sizes[i]) } plot.args <- list( x, line.color.by='bootstrap', line.color.scale=scale_colour_gradient(limits=c(50, 100), low='red', high='black'), node.size.by='pop.size', node.size.scale = scale_size_continuous(limits=c(0, 100000), range=c(1, 5)), label.size=2 ) do.call(ggphylo, plot.args) } draw_ggphylo(x, bootstraps, pop.sizes) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.