Network visualization in the 'ggplot2' framework. Network functionality is provided in a single 'ggplot2' layer by calling the geom 'net'. Layouts are calculated using the 'sna' package, example networks are included.
geomnet is a package built on top of the most recent major
ggplot2 release. It provides a
geom_net to visualize graphs and networks. It also include the function
stat_net to calculate network layouts with the
sna package. Finally, the function
geom_circle is included to draw circles using
You can install
geomnet directly from CRAN
install.packages('geomnet') or from Github
This example shows the theme inheritance properties of the theme elements of
ggplot2. Note: this example has not been updated since the release of
ggplot2 2.2.0 and as such the content may have changed.
library(dplyr)library(geomnet)data(theme_elements)TEnet <- fortify(as.edgedf(theme_elements$edges[,c("parent", "child")]), theme_elements$vertices)# create a degree variable for use laterTEnet <- TEnet %>%group_by(from_id) %>%mutate(degree = sqrt(10 * n() + 1))# plotggplot(data = TEnet,aes(from_id = from_id, to_id = to_id)) +geom_net(layout.alg = "fruchtermanreingold",aes(fontsize = degree), directed = TRUE,labelon = TRUE, size = 1, labelcolour = 'black',ecolour = "grey70", arrowsize = 0.5,linewidth = 0.5, repel = TRUE) +theme_net() +xlim(c(-0.05, 1.05))
In this example, we reimagine the traditional blood donation diagram as a directed network. Arrows point to the blood type that receives. This example provides two data frames to
library(geomnet)data(blood)ggplot(data = blood$edges, aes(from_id = from, to_id = to)) +geom_net(colour = "darkred", layout.alg = "circle", labelon = TRUE,size = 15, directed = TRUE, vjust = 0.5, labelcolour = "grey80",arrowsize = 1.5, linewidth = 0.5, arrowgap = 0.05,selfloops = TRUE, ecolour = "grey40") +theme_net()
In this fun example from this website, there is a tie between two students if one provides emotional support to the other at some point in the book. It is a directed network, so in the visualization, the arrow points to the student receiving support.
library(geomnet)head(hp.edges)#> name1 name2 book#> 1 Dean Thomas Harry James Potter 1#> 2 Dean Thomas Hermione Granger 1#> 3 Dean Thomas Neville Longbottom 1#> 4 Dean Thomas Ronald Weasley 1#> 5 Dean Thomas Seamus Finnigan 1#> 6 Fred Weasley George Weasley 1head(hp.chars)#> name schoolyear gender house#> 1 Adrian Pucey 1989 M Slytherin#> 2 Alicia Spinnet 1989 F Gryffindor#> 3 Angelina Johnson 1989 F Gryffindor#> 4 Anthony Goldstein 1991 M Ravenclaw#> 5 Blaise Zabini 1991 M Slytherin#> 6 C. Warrington 1989 M Slytherinhp.all <- fortify(as.edgedf(hp.edges), hp.chars, group = "book")#> Using name1 as the from node column and name2 as the to node column.#> If this is not correct, rewrite dat so that the first 2 columns are from and to node, respectively.#> Joining edge and node information by from_id and name respectively.# only plot the characters with any connections in a given book.ggplot(data=hp.all, aes(from_id = from, to_id = to_id)) +geom_net(fiteach=T, directed = T, size = 3, linewidth = .5,ealpha = .5, labelon = T, fontsize = 3, repel = T,labelcolour = "black", arrowsize = .5, singletons = FALSE,aes(colour = house, group = house, shape = gender)) +scale_colour_manual(values = c("#941B08","#F1F31C","#071A80", "#154C07")) +facet_wrap(~book, labeller = "label_both") +theme_net() + theme(panel.background = element_rect(colour = 'black'))
Now including interactivity with
library(geomnet)# devtools::install_github("ropensci/plotly")library(plotly)data("football")# data step: merge vertices and edgesftnet <- fortify(as.edgedf(football$edges), football$vertices)# create data plotgg <- ggplot(data = ftnet,aes(from_id = from_id, to_id = to_id)) +geom_net(layout.alg = 'fruchtermanreingold',aes(colour = value, group = value,linetype = factor(same.conf != 1)),linewidth = 0.5,size = 5, vjust = -0.75, alpha = 0.3) +theme_net() +theme(legend.position = "bottom") +scale_colour_brewer("Conference", palette = "Paired") +guides(linetype = FALSE)ggplotly(gg)
This version of geomnet has been updated to account for changes in ggplot2 version 2.2.0.
"edgedf"classes for newly added
fortifymethods included for
"edgedf"class objects to transform network objects to data frame objects that can be visualized using
plotlyfunctionality. Hovertext describes edges and provides node labels. Note that arrows are not currently supported in
plotly, so directed networks will appear as undirected.
singletonsadded for plotting all nodes in a network even if they have indegree and outdegree of zero. This is most likely to be useful when facetting. The default is
TRUE, so all nodes are plotted in all panels. When
singletons = FALSE, nodes are only plotted in the panel(s) in which they have at least one incoming or outgoing edge.
labelon = TRUEto plot labels with the node ids. Use
aes()to map a variable value to node label.
selfiesargument for drawing self-loops on nodes has been changed to
ggnetworkby François Briatte