YuLab-SMU / ggtree

:christmas_tree:Visualization and annotation of phylogenetic trees
https://yulab-smu.top/contribution-tree-data/
839 stars 173 forks source link

facet_widths() are not compatiable with plotly. Request to make annotated phylogenies interactive. #478

Open magnusnosnes opened 2 years ago

magnusnosnes commented 2 years ago

Hi! First of all, thanks for producing such a great visualization tool for phylogenies!

I am using ggtree with plotly to produce some interactive views of a huge phylogeny with metadata: see my attempt at: Interactive phylogeny. When I plot the tree with metadaata using ggplotly, I am unable to scale the size of the facets. I would like to reduce the width of the metadata-facets so that the tree (which is of main interest) takes up most of the figure.

The issue is that facet_widths() works for a static figure, but doesn't work with plotly (ggplotly). It would be great with an option to do this because it would allow me to make interactive visualizations of very large trees with metadata, where the user can zoom and explore interesting clades.

Here is a reproducible example:

#LOAD LIBS ---------------------------------------------------------------
library(ape)
library(ggtree)
library(plotly)
#CREATE A TREE -------------------------------------------------------------
n_samples <- 20
n_grp <- 4
tree <- ape::rtree(n = n_samples)

#CREATE SOME METADATA ----------------------------------------------------
id <- tree$tip.label
set.seed(42)
grp <- sample(LETTERS[1:n_grp], size = n_samples, replace = T)
dat <- tibble::tibble(id = id,
                     grp = grp, dummy_bar_value = rep(1, length(grp)))

#PLOT THE TREE -----------------------------------------------------------
p1 <- ggtree(tree)
metat <- p1$data %>%
 dplyr::inner_join(dat, c('label' = 'id'))

df_bar = data.frame(id=metat$label, dummy_bar_value=rep(1, length(grp)))

p2 <- p1
p2 = p2 %<+% metat

p2 = p2+geom_facet(panel = "Trait1",data =df_bar, geom = geom_col,
                 aes(x = df_bar$dummy_bar_value, fill = grp
                 ), orientation = 'y', width = 1)+
  geom_facet(panel = "Trait2",data =df_bar, geom = geom_col,
                 aes(x = df_bar$dummy_bar_value, fill = grp
                 ), orientation = 'y', width = 1)+theme_tree2()

#SCALE FACET WIDTHS -----------------------------------------------------------

ggplotly(p2) #Works fine! : interactive annotated phylogeny

p3=facet_widths(p2,c(Tree=12)) #Reduce width of metadata facets. 
p3 #The annotation looks great at this point. 

ggplotly(p3) #The scaled version doesn't work with plotly: see the warning message below.

Warning message:
In geom2trace.default(dots[[1L]][[1L]], dots[[2L]][[1L]], dots[[3L]][[1L]]) :
  geom_GeomCustomAnn() has yet to be implemented in plotly.
  If you'd like to see this geom implemented,
  Please open an issue with your example code at
  https://github.com/ropensci/plotly/issues

Thanks in advance, Best regards, Magnus Nygård Osnes