Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Created February 10, 2015 03:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timelyportfolio/1de4cdd99d92848bc625 to your computer and use it in GitHub Desktop.
Save timelyportfolio/1de4cdd99d92848bc625 to your computer and use it in GitHub Desktop.
some examples of plotting phylo with DiagrammeR + ape + igraph
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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment