Learn R Programming

geomnet

geomnet is a package built on top of the most recent major ggplot2 release. It provides a ggplot2 geom called 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 ggplot2.

You can install geomnet directly from CRAN install.packages('geomnet') or from Github devtools::install_github("sctyner/geomnet")

Examples

ggplot2 Theme Elements

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)
# data step
TEnet <- fortify(as.edgedf(theme_elements$edges[,c("parent", "child")]), theme_elements$vertices)
# create a degree variable for use later
TEnet <- TEnet %>%
  group_by(from_id) %>%
  mutate(degree = sqrt(10 * n() + 1))
# plot
ggplot(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))

Blood Donation Diagram

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 geom_net().

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() 

Harry Potter Peer Support Network

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    1
head(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  Slytherin
hp.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'))

Interactivity with plotly!

Now including interactivity with ggplotly()!

# blood donation example
library(geomnet)
# devtools::install_github("ropensci/plotly")
library(plotly)
data(blood)
bloodnet <- fortify(as.edgedf(blood$edges), blood$vertices)
p <- ggplot(data = bloodnet, aes(from_id = from_id, to_id = to_id))

# create data plot
p2 <- p + geom_net(aes(size=Predominance, colour=type, shape=rho, linetype=group_to),
             linewidth=0.75, labelon =TRUE, directed = TRUE, labelcolour="black") +
  facet_wrap(~Ethnicity) +
  scale_colour_brewer(palette="Set2") 
ggplotly(p2) %>% hide_legend()

# Classic College Football Example
data("football")
# data step: merge vertices and edges
ftnet <- fortify(as.edgedf(football$edges), football$vertices)

# create data plot
gg <- 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)

Copy Link

Version

Install

install.packages('geomnet')

Monthly Downloads

61

Version

0.3.1

License

GPL (>= 2)

Issues

Pull Requests

Stars

Forks

Maintainer

Last Published

November 26th, 2020

Functions in geomnet (0.3.1)

fortify.adjmat

Function for converting a network adjacency matrix into the correct format for use with geomnet
fortify.edgedf

Function for converting a network edge list in data frame form into the correct format for use with geomnet
jtt

Number of JTTs in a graph
lesmis

Co-appearance network of characters in Les Miserables (undirected)
football

College football games network (undirected)
as.adjmat

Cast matrix to an adjacency matrix
email

Email network (directed)
madmen

Network of romantic relationships in the TV show Mad Men (undirected)
as.edgedf

Cast a data frame to an edgedf
metro_map

Map of Washington DC Metro area
soccer

High school boys' soccer games in Iowa (undirected)
mm.directed

A directed network of Mad Men relationships
protein

A protein interaction network (undirected)
theme_elements

ggplot2 theme attribute inheritance network (directed)
fortify.igraph

Function for converting an igraph object into the correct format for use with geomnet
fortify.network

Function for converting a network object into the correct format for use with geomnet
GeomCircle

Geom for drawing circles in the ggplot2 framework
blood

Network of blood types (directed)
geom_net

Networks
theme_net

Theme for network visualization
bikes

Bike sharing network (directed)