ggtree+ggplotly: show custom images on hover

85 views
Skip to first unread message

Ales Bucek

unread,
May 14, 2020, 1:37:14 PM5/14/20
to ggtree
Dear Yu,
I'm trying to use combination of ggtree and ggplotly for interactive phylogenies.
The ggtree package is apparently not fully supported by plotly (https://github.com/ropensci/plotly/issues/566) - i.e. not supported geoms, but it still feels like a very powerful tool to share phylogenies and rich metadata associated with the phylogenies.

1st question: Did you previously try to use ggplotly with ggtree and would you consider writing a tutorial on combination of these two? I'd be happy to help.

2nd question: One feature I'm trying to test is a photo of species shown when hovering over the respective tip.
I can do this as in example below:
library(dplyr)
library
(ggtree)
library
(plotly)

#obtain image

download
.file("https://upload.wikimedia.org/wikipedia/commons/thumb/1/1b/R_logo.svg/200px-R_logo.svg.png", "Rlogo.png")
Rlogo <- base64enc::dataURI(file = sprintf("Rlogo.png"))

#plot tree

nwk
<- system.file("extdata", "sample.nwk", package="treeio")
tree
<- read.tree(nwk)
p
<- ggtree(tree)

#generate vector of tip coordinates
xcoords
<- subset(p$data, isTip==TRUE)$x
ycoords
<- subset(p$data, isTip==TRUE)$y

#generate interactive plot
p
%>% ggplotly() %>%
  add_text
(inherit=FALSE, x=xcoords, y=ycoords, customdata = Rlogo, text = FALSE) %>%
  htmlwidgets
::onRender(readLines("tooltip-image.js"))

But this fails when I add geom_point() to the tree, because the only the node information is shown on hover instead of trigerring the on-hover action to render the image.
p <- ggtree(tree)+geom_point()
p
%>% ggplotly() %>%
  add_text
(inherit=FALSE, x=xcoords, y=ycoords, customdata = Rlogo, text = FALSE) %>%
  htmlwidgets
::onRender(readLines("tooltip-image.js"))
Ideally, I would like to show both the node information and also an associated image data on hover. While this is a very specific question I'm in general interested some recommended practises could be formulate how to combine ggtree and ggplotly.

Thanks for any suggestions!
Ales

Reply all
Reply to author
Forward
0 new messages