ggraph: Grammar of Graph Graphics

188 views
Skip to first unread message

Thomas

unread,
Jan 13, 2016, 8:03:08 AM1/13/16
to ggplot2
Hot on the heels of a lot of great ggplot2 extension packages I'm happy to announce the availability of the alpha version of ggraph (https://github.com/thomasp85/ggraph).

ggraph is a package that extends ggplot2 for graph and network visualization. To be clear it is not a package that use ggplot2 underneath to create the plots, rather a ggplot2-like API for creating graph visualizations. Currently it supports data in the form of igraph and dendrogram objects, but others might be added later.

A quick example of the API:

# Let's use iris as we all love the iris dataset
## Perform hierarchical clustering on the iris data
irisDen <- as.dendrogram(hclust(dist(`rownames<-`(iris[1:4], iris[,5]), method='euclidean', ), method='ward.D2'))
## Add the species information to the leafs
irisDen <- dendrapply(irisDen, function(d) {
    if(is.leaf(d)) 
        attr(d, 'nodePar') <- list(species=iris[as.integer(attr(d, 'label')),5])
    d
})

# Plotting this looks very much like ggplot2 except for the new geoms
ggraph(graph = irisDen, layout = 'dendrogram', repel = TRUE, circular = TRUE, 
       ratio = 2) + 
    geom_edge_elbow() + 
    geom_node_text(aes(x = x*1.05, y=y*1.05, filter=leaf, 
                       angle = atan(y/x)*360/(2*pi), hjust='outward'), 
                   size=3) + 
    geom_node_point(aes(filter=leaf, color=species)) + 
    coord_fixed()


I'll be happy to take any feedback and hope that it will prove useful to a lot of you...


best


Thomas




Wouter van der Bijl

unread,
Jan 13, 2016, 12:12:47 PM1/13/16
to ggplot2
Hi Thomas,

This looks really promising! Any plans to support the plotting of phylogenetic trees, such as the phylo object from package ape?

Cheers,
W.

Thomas

unread,
Jan 13, 2016, 2:26:27 PM1/13/16
to ggplot2
Hi Wouter

In short, yes - Currently support for dendrogram and igraph objects exists, but other graph like objects will be added. Doesn't there exist coercion functions for phyla object to one of the two in the meantime? I think there does...

best

Thomas

Julio Rojas

unread,
Jan 17, 2016, 2:46:29 PM1/17/16
to Thomas, ggplot2
Whoa Thomas, almost in time for a paper I am finishing. Will it support automatic layout functions like igraph does? Node sizes via functions (pagerank, degree)?

Keep the good work up!

-------------------------------------------------
Julio Rojas
jcred...@gmail.com

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Thomas

unread,
Jan 17, 2016, 3:21:42 PM1/17/16
to ggplot2, thom...@gmail.com
Yes, yes :-)

All igraph layouts are supported as well as an additional one for trees/DAGs (almost equivalent to D3s layout.cluster). In addition it is possible to create your own layout functions and have them handled automatically by ggraph.

Nodes can be styled on any parameter returned by the layout as well as any parameter inherited by the graph. For dendrograms these are the parameters stored in the nodePar attribute. For igraph objects it is everything stored as node attributes. Currently to map node size to e.g. degree it is necessary to calculate it and store it on the graph object prior to plotting e.g.

V(gr)$degree <- degree(gr)

While it would seem nice to have this sort of thing happen automagically I think it would convolute what is happening, as well as tie ggraph unnecessarily tight to igraph...

Please send me a link if you end up using ggraph in an article :-)

best

Thomas
Reply all
Reply to author
Forward
0 new messages