Created
February 10, 2015 03:56
-
-
Save timelyportfolio/1de4cdd99d92848bc625 to your computer and use it in GitHub Desktop.
some examples of plotting phylo with DiagrammeR + ape + igraph
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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