block by timelyportfolio 1de4cdd99d92848bc625

some examples of plotting phylo with DiagrammeR + ape + igraph

code.R

library(ape)
library(DiagrammeR)
library(igraph)
library(htmltools)
library(pipeR)

# use this since write.igraph needs a file
tmp <- tempfile()

data(bird.orders)
class(bird.orders)
plot(bird.orders, cex = 0.4)

# convert to igraph
iG <- as.igraph(bird.orders)
# change name to label for grViz
V(iG)$label <- V(iG)$name
remove.vertex.attribute(iG,name="name")
write.graph( iG , file = tmp, format = "dot" )


tl1 <- tagList(
  lapply(
    # get an error with circo but worked with smaller
    c('dot','neato','circo', 'twopi')
    #circo doesn't work
    ,function(eng){
      grViz( tmp, engine = eng )
    }
  )
)
#html_print(tl1)

tl2 <- bird.orders %>>%
  (
    sapply(
      1:nrow(.$edge)
      ,function(e){
        paste0(
          .$edge[e,1]
          ,"-->"
          ,if(is.na(.$tip.label[.$edge[e,2]])){
            .$edge[e,2]            
          } else{
            .$tip.label[.$edge[e,2]]
          }
        )
      }
    )
  ) %>>%
  ( c( "graph LR", . ) ) %>>%
  (paste0(.,collapse=";\n")) %>>%
  mermaid
#tl2

# another example with a bigger phylo and grViz
data(bird.families)
class(bird.families)

plot(bird.families, cex = 0.4)


# convert to igraph
iG <- as.igraph(bird.families)
# change name to label for grViz
V(iG)$label <- V(iG)$name
remove.vertex.attribute(iG,name="name")
write.graph( iG , file = tmp, format = "dot" )


tl3 <- tagList(
  lapply(
    # get an error with circo
    c('dot','neato','circo', 'twopi')
    ,function(eng){
      grViz( tmp, engine = eng )
    }
  )
)
#html_print(tl3)

html_print(tagList(tl1,tl2,tl3))