xlim_tree not changing the tree

2 views
Skip to first unread message

Jaime Cordova

unread,
Nov 22, 2025, 11:04:03 PM (18 hours ago) Nov 22
to ggtree
Hello, 

I'm working to make a two-panel figure, with a tip labeled tree and a stacked bar graph. However, the tip labels get cut off. I found xlim_tree, but it's not working. I'm still able to make the figure, but it is simply not changing the tree's xlim. I've attached the code with the 3 different attempts based on the guides I've found. I've also copied it below. Thank you for the help! 

library(tidyr)
library(dplyr)
library(reshape2)
library(ggplot2)
library(ggstance)

xenoGI_TotalEventProportion_Long <- xenoGI_TotalEventProportion %>%
        pivot_longer(cols = -strain, names_to = "Event", values_to = "Proportion")

Attempt A
xenoGI_Tree_Plot <- ggtree(xenoGI_Tree_v2.0)
xenoGI_Tree_Plot_Labeled_xlimtree <-  xenoGI_Tree_Plot + geom_tiplab(size = 3.4) + xlim_tree(4)

facet_plot(xenoGI_Tree_Plot_Labeled_xlimtree, panel = 'Stacked Barplot',
           data = xenoGI_TotalEventProportion_Long,
           geom = geom_barh,
           mapping = aes(
                   x = Proportion,
                   fill = Event),
           stat = 'identity')

Attempt B

ggtree(xenoGI_Tree_v2.0, branch.length = 'none') + geom_tiplab(size = 3.4) + xlim_tree(4) geom_facet(panel = "Stacked Bar",
                                       data = xenoGI_TotalEventProportion_Long,
                                       geom = geom_barh,
                                       mapping = aes(
                                       x = Proportion,
                                       fill = Event),
                                       stat = 'identity')

Attempt C
xenoGI_Tree_Plot <- ggtree(xenoGI_Tree_v2.0)
xenoGI_Tree_Plot_Labeled <-  xenoGI_Tree_Plot + geom_tiplab(size = 3.4)

xenoGI_Tree_Plot_Stacked <- facet_plot(xenoGI_Tree_Plot_Labeled, panel = "Stacked Bar",
           data = xenoGI_TotalEventProportion_Long,
           geom = geom_barh,
           mapping = aes(
                   x = Proportion,
                   fill = Event),
           stat = 'identity')

xenoGI_Tree_Plot_Stacked + xlim_tree(6)

Kind regards,
Jaime 
xenoGI_EventTree.R

Luke Baton

unread,
12:38 PM (5 hours ago) 12:38 PM
to ggtree
Jamie,

As you didn't provide your original tree and associated data, it's not possible to reproduce your example, and so understand why things aren't working for you, but, if random input is created as shown below, all three of your scripts work fine for me. The only problem I noticed was that there was a "+" missing from your "Attempt B" between the xlim_tree() and geom_facet() functions.

For example: "Attempt B"

> library(ape)
> library(ggplot2)
> library(ggtree)
> library(ggstance)

> set.seed(1)
> myTree <- rtree(7)

> set.seed(1)
> myData <- data.frame(id = rep(myTree$tip.label, each = 3), Event = c("A", "B", "C"), Proportion = round(runif(21, min = 0, max = (1/3)), 2))

> ggtree(myTree, branch.length = 'none') + geom_tiplab(size = 3.4) + xlim_tree(8) + geom_facet(panel = "Stacked Bar", data = myData, geom = geom_barh, mapping = aes(x = Proportion, fill = Event), stat = 'identity') + theme_bw()

[See attached image]

Note that I added "+ theme_bw()" at the end of your ggtree() call, as this plots an x-axis on the tree panel, which is useful for determining the best value for xlimtree (and seeing the effect of changing xlimtree).

I can only think of two explanations for your problem:

(i) The value of xlimtree that you are using is too low, and is actually below the height of your tree (so you need to use a higher value).

(ii) Try reinstalling the ggtree package, and make sure that you are using the latest version.

Luke 
Attempt B.png
Reply all
Reply to author
Forward
0 new messages