### Install ggtreeExtra from GitHub Source: https://github.com/yulab-smu/ggtreeextra/blob/master/README.md Install the development version of ggtreeExtra from GitHub. Requires the devtools package. ```r if (!requireNamespace("devtools", quietly=TRUE)) install.packages("devtools") devtools::install_github("xiangpin/ggtreeExtra") ``` -------------------------------- ### Install ggtreeExtra from Bioconductor Source: https://github.com/yulab-smu/ggtreeextra/blob/master/README.md Install the released version of ggtreeExtra from Bioconductor. Ensure BiocManager is installed. ```r if (!requireNamespace("BiocManager", quietly=TRUE)) install.packages("BiocManager") ## BiocManager::install("BiocUpgrade") ## you may need this BiocManager::install("ggtreeExtra") ``` -------------------------------- ### Install ggtreeExtra from Bioconductor and GitHub Source: https://context7.com/yulab-smu/ggtreeextra/llms.txt Installs the release version of ggtreeExtra from Bioconductor and the development version from GitHub. Ensure remotes and BiocManager are installed first. ```r if (!requireNamespace("BiocManager", quietly = TRUE)) install.packages("BiocManager") BiocManager::install("ggtreeExtra") ``` ```r if (!requireNamespace("remotes", quietly = TRUE)) install.packages("remotes") remotes::install_github("YuLab-SMU/ggtreeExtra") ``` -------------------------------- ### Add Star Points Layer to Fan Tree with ggtreeExtra Source: https://context7.com/yulab-smu/ggtreeextra/llms.txt Demonstrates adding a layer of star plots to a fan-layout phylogenetic tree using geom_fruit. Requires loading tree and data, then applying geom_star with specific aesthetic mappings and scales. ```r library(ggtreeExtra) library(ggtree) library(ggplot2) library(ggstar) library(ggnewscale) library(treeio) # Load bundled example data trfile <- system.file("extdata", "tree.nwk", package = "ggtreeExtra") tippoint <- system.file("extdata", "tree_tippoint_bar.csv", package = "ggtreeExtra") ring1file <- system.file("extdata", "first_ring_discrete.csv", package = "ggtreeExtra") ring2file <- system.file("extdata", "second_ring_continuous.csv", package = "ggtreeExtra") tree <- read.tree(trfile) dat1 <- read.csv(tippoint) # columns: ID, Location, Length, Group, Abundance dat2 <- read.csv(ring1file) # columns: ID, Pos, Type dat3 <- read.csv(ring2file) # columns: ID, Type2, Alpha # Base fan-layout tree p <- ggtree(tree, layout = "fan", open.angle = 10, size = 0.5) # Layer 1 – star points at tips (position="identity" keeps them on tip nodes) p2 <- p + geom_fruit( data = dat1, geom = geom_star, mapping = aes(y = ID, fill = Location, size = Length, starshape = Group), position = "identity", starstroke = 0.2 ) + scale_size_continuous(range = c(1, 3), guide = guide_legend(keywidth = 0.5, keyheight = 0.5, override.aes = list(starshape = 15), order = 2)) + scale_fill_manual( values = c("#F8766D","#C49A00","#53B400","#00C094","#00B6EB","#A58AFF","#FB61D7"), guide = "none") + scale_starshape_manual(values = c(1, 15), guide = guide_legend(keywidth = 0.5, keyheight = 0.5, order = 1), na.translate = FALSE) ``` -------------------------------- ### Add Continuous Heatmap Ring with X-axis Annotation using ggtreeExtra Source: https://context7.com/yulab-smu/ggtreeextra/llms.txt Adds a continuous heatmap ring with alpha transparency and x-axis labels. This snippet demonstrates using geom_tile with new scale for fill and alpha, and configuring axis parameters for labels. ```r p4 <- p3 + new_scale_fill() + geom_fruit( data = dat3, geom = geom_tile, mapping = aes(y = ID, x = Type2, alpha = Alpha, fill = Type2), pwidth = 0.15, axis.params = list( axis = "x", # show x-axis labels text.angle = -45, hjust = 0 ) ) + scale_fill_manual( values = c("#b22222","#005500","#0000be","#9f1f9f"), guide = guide_legend(keywidth = 0.5, keyheight = 0.5, order = 4)) + scale_alpha_continuous(range = c(0, 0.4), guide = guide_legend(keywidth = 0.5, keyheight = 0.5, order = 5)) ``` -------------------------------- ### Bar Chart with position_dodgex Source: https://context7.com/yulab-smu/ggtreeextra/llms.txt Demonstrates using position_dodgex for standard and vertically/horizontally expanded dodging in bar charts. Useful for controlling spacing and alignment. ```R p1 <- ggplot(iris, aes(x = Species, y = Petal.Length, fill = ID)) + geom_bar(stat = "identity", position = position_dodgex()) p2 <- ggplot(iris, aes(x = Species, y = Petal.Length, fill = ID)) + 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)) p5 <- ggplot(iris, aes(x = Species, y = Petal.Length, fill = ID)) + geom_boxplot(position = position_dodgex2(vexpand = 5)) (p1 + p2 + p3) / p5 ``` -------------------------------- ### Combined Dodge and Jitter with position_jitterdodgex Source: https://context7.com/yulab-smu/ggtreeextra/llms.txt Illustrates using position_jitterdodgex to simultaneously dodge and jitter points, suitable for boxplots where points need to be spread within dodge groups. Requires a grouping aesthetic. ```R library(ggplot2) library(ggtreeExtra) set.seed(123) df <- data.frame( x = rep(c("A","B","C"), each = 40), y = rnorm(120), group = rep(c("g1","g2"), 60) ) ggplot(df, aes(x = x, y = y, fill = group, color = group)) + geom_boxplot(position = position_dodgex(), outlier.shape = NA) + geom_point(shape = 21, size = 1.5, position = position_jitterdodgex( jitter.width = 0.1, dodge.width = 0.75, vexpand = 0 # no global shift )) ``` -------------------------------- ### Stack Multiple Geoms on Same Ring with geom_fruit_list Source: https://context7.com/yulab-smu/ggtreeextra/llms.txt Groups multiple geom_fruit layers and scales to render at the same radial position. Use when overlaying different geoms like bars and points that should share panel space. ```R library(ggtreeExtra) library(ggtree) library(ggplot2) library(ggstar) library(ggnewscale) set.seed(1024) tr <- rtree(100) dt <- data.frame(id = tr$tip.label, value = abs(rnorm(100)), group = c(rep("A", 50), rep("B", 50))) df <- dt; colnames(df)[3] <- "group2" dtf <- dt; colnames(dtf)[3] <- "group3" # Base fan tree p <- ggtree(tr, layout = "fan", open.angle = 0) # Ring 1: simple bar chart p1 <- p + geom_fruit(data = dt, geom = geom_col, mapping = aes(y = id, x = value, fill = group)) + new_scale_fill() # Ring 2: bar + stars occupying the SAME outer ring via geom_fruit_list p2 <- p1 + geom_fruit_list( geom_fruit(data = df, geom = geom_col, mapping = aes(y = id, x = value, fill = group2)), scale_fill_manual(values = c("blue", "red")), new_scale_fill(), geom_fruit(data = dt, geom = geom_star, mapping = aes(y = id, x = value, fill = group), size = 2.5, color = NA, starstroke = 0) ) + new_scale_fill() # Ring 3: another bar chart on its own ring p3 <- p2 + geom_fruit(data = dtf, geom = geom_col, mapping = aes(y = id, x = value, fill = group3)) + scale_fill_manual(values = c("#00AED7", "#009E73")) p3 ``` -------------------------------- ### Add Discrete Heatmap Ring with ggtreeExtra Source: https://context7.com/yulab-smu/ggtreeextra/llms.txt Adds a discrete heatmap ring as an external layer to a phylogenetic tree. Uses geom_tile and requires specifying offset and panel width relative to the tree's x-range. ```r p3 <- p2 + new_scale_fill() + geom_fruit( data = dat2, geom = geom_tile, mapping = aes(y = ID, x = Pos, fill = Type), offset = 0.08, # gap between this ring and the previous layer pwidth = 0.25 # ring width = 0.25 * x-range of tree ) + scale_fill_manual(values = c("#339933", "#dfac03"), guide = guide_legend(keywidth = 0.5, keyheight = 0.5, order = 3)) ``` -------------------------------- ### Jittered Points Overlaid on Boxplots with position_jitterx Source: https://context7.com/yulab-smu/ggtreeextra/llms.txt Shows how to overlay jittered points on boxplots using geom_fruit and position_jitterx. Requires setting a seed for reproducibility and ensures points are within their respective bands. ```R library(ggtreeExtra) library(ggtree) library(ggplot2) set.seed(1024) tr <- rtree(10) df <- data.frame(id = tr$tip.label, group = rep(c("A","B"), 5)) dat <- data.frame(id = rep(tr$tip.label, 8), value = rnorm(80, 0.5, 0.15)) dt <- merge(dat, df, by = "id") p <- ggtree(tr) %<+% df + geom_tiplab(align = TRUE, linesize = .1, size = 3) # Boxplot layer gf1 <- geom_fruit(data = dat, geom = geom_boxplot, mapping = aes(x = value, y = id), orientation = "y", offset = 0.1, pwidth = 0.9) # Overlaid jittered points at the same x-range set.seed(1024) gf2 <- geom_fruit(data = dt, geom = geom_point, mapping = aes(x = value, y = id, color = group), offset = 0.1, pwidth = 0.9, position = position_jitterx(height = 0.3), axis.params = list(axis = "x", text.size = 2), grid.params = list()) # Both layers share the same panel via geom_fruit_list p + geom_fruit_list(gf1, gf2) ``` -------------------------------- ### Identity Position with Global Shift using position_identityx Source: https://context7.com/yulab-smu/ggtreeextra/llms.txt Demonstrates position_identityx as a replacement for position_identity, allowing global horizontal (hexpand) or vertical (vexpand) shifts for geoms. Useful for precise layer alignment. ```R library(ggplot2) library(ggtreeExtra) library(patchwork) # Standard identity position p1 <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point(position = position_identityx()) + ylim(0, 50) # Shift entire point layer up by 5 units p2 <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point(position = position_identityx(vexpand = 5)) + ylim(0, 50) # Horizontal shift p3 <- ggplot(mtcars, aes(y = wt, x = mpg)) + geom_point(position = position_identityx(hexpand = 5)) + xlim(0, 50) p1 + p2 + p3 ``` -------------------------------- ### Sine-based Point Positioning for Ridgelines with position_points_sinax Source: https://context7.com/yulab-smu/ggtreeextra/llms.txt Positions points within ridgeline density bands using a sine distribution, controlled by rel_min/rel_max and hexpand for horizontal alignment. Ideal for overlaying points on density plots. ```R library(ggplot2) library(ggtreeExtra) library(ggridges) # for geom_density_ridges_gradient set.seed(42) df <- data.frame( species = sample(c("A","B","C"), 300, replace = TRUE), value = rnorm(300) ) ggplot(df, aes(x = value, y = species, fill = after_stat(x))) + geom_density_ridges_gradient(scale = 1.5) + geom_point(aes(x = value, y = species), position = position_points_sinax( rel_min = 0.02, rel_max = 0.98, hexpand = 0 ), size = 0.8, alpha = 0.4) + scale_fill_viridis_c() ``` -------------------------------- ### Pipe-Friendly Alias for geom_fruit() with fruit_plot() Source: https://context7.com/yulab-smu/ggtreeextra/llms.txt Uses fruit_plot, a convenience alias for geom_fruit, which accepts the tree plot as the first argument, making it suitable for magrittr pipe chains. ```R library(ggtreeExtra) library(ggtree) library(ggplot2) library(magrittr) set.seed(42) tr <- rtree(50) dd <- data.frame(id = tr$tip.label, score = abs(rnorm(50))) ggtree(tr, layout = "circular") %>% fruit_plot( data = dd, geom = geom_col, mapping = aes(y = id, x = score), pwidth = 0.3, offset = 0.05 ) ``` -------------------------------- ### Stack Bar Charts with Optional Global Shift using position_stackx Source: https://context7.com/yulab-smu/ggtreeextra/llms.txt Extends ggplot2::position_stack with hexpand and vexpand for shifting stacked layers. This is the default position for geom_bar and geom_col in geom_fruit. ```R library(ggplot2) library(ggtreeExtra) df <- data.frame(trt = c("a","b","c"), outcome = c(2.3, 1.9, 3.2)) # Standard stack p1 <- ggplot(df, aes(x = trt, y = outcome)) + geom_bar(stat = "identity", position = position_stackx()) # Shift entire stacked layer up by 5 units on the y-axis p2 <- ggplot(df, aes(x = trt, y = outcome)) + geom_bar(stat = "identity", position = position_stackx(vexpand = 5)) # Horizontal orientation with horizontal shift p3 <- ggplot(df, aes(x = outcome, y = trt)) + geom_bar(stat = "identity", orientation = "y", position = position_stackx(hexpand = 5)) library(patchwork) p1 + p2 + p3 ``` -------------------------------- ### Add Bar Chart with Grid Lines to Tree Source: https://context7.com/yulab-smu/ggtreeextra/llms.txt Adds a bar chart layer with custom fill colors and legend options to a tree plot. Grid lines are enabled by default. ```R p5 <- p4 + new_scale_fill() + geom_fruit( data = dat1, geom = geom_col, mapping = aes(y = ID, x = Abundance, fill = Location), pwidth = 0.4, axis.params = list(axis = "x", text.angle = -45, hjust = 0), grid.params = list() # default grey grid lines ) + scale_fill_manual( values = c("#F8766D","#C49A00","#53B400","#00C094","#00B6EB","#A58AFF","#FB61D7"), guide = guide_legend(keywidth = 0.5, keyheight = 0.5, order = 6)) theme(legend.background = element_rect(fill = NA), legend.title = element_text(size = 7), legend.text = element_text(size = 6), legend.spacing.y = unit(0.02, "cm")) p5 ``` -------------------------------- ### Dodge Geoms with Optional Global Shift using position_dodgex Source: https://context7.com/yulab-smu/ggtreeextra/llms.txt Extends ggplot2::position_dodge and position_dodge2 with hexpand/vexpand offsets. These are the default positions for geom_violin and geom_boxplot in geom_fruit. ```R library(ggplot2) library(ggtreeExtra) library(patchwork) iris$ID <- rep(c(rep("test1",15), rep("test2",15), rep("test3",20)), 3) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.