library(learnr) library(usethis) library(tidyverse) library(tidygraph) library(igraph) library(ggraph) library(ggnetwork) library(phyloseq) knitr::opts_chunk$set(echo = FALSE, highlight = TRUE, message=FALSE, warning=FALSE)
In this worksheet, we will discuss how to visualize networks using the tidyverse.
We will be using the R package tidyverse, which includes ggplot()
and related functions.
# load required library library(tidyverse)
We use specialized packages for graphs based on
the igraph
package, the tidyverse compatible ones
are ggnetwork
and ggraph
.
# load required library library(igraph) library(tidygraph) library(ggnetwork) library(ggraph)
We will use a package which is on BIOCONDUCTOR and not on CRAN, so you need to also install it using:
if (!requireNamespace("BiocManager", quietly = TRUE)) install.packages("BiocManager") BiocManager::install("phyloseq")
library("igraph") edges1 = matrix(c(1,3,2,3,3,4,4,5,4,6), byrow = TRUE, ncol = 2) g1 = graph_from_edgelist(edges1, directed = FALSE) vertex_attr(g1, name = "vname") = paste0(1:6) plot(g1, vertex.size = 25, edge.width = 5, vertex.color = "coral")
library(reshape) ggplotadjacency = function(a){ n = nrow(a) p = ncol(a) melted_a = melt(a) melted_a$value = as.factor(melted_a$value) cols = c("white", "darkblue") ggplot(data = melted_a, aes(x = X1, y = X2, fill=value)) + geom_tile(colour="black") + coord_fixed(ratio = 1,ylim=c(0.5,n+0.5),xlim=c(0.5,p+0.5))+ scale_fill_manual(values=cols)+scale_x_discrete(name="",breaks=1:p)+ scale_y_reverse(name="",breaks=n:1)+theme_bw() + theme(axis.text = element_text(size = 10), legend.key = element_rect(fill = "white"), legend.background = element_rect(fill = "white"), panel.border=element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line =element_line(color="white") ) }
Here is the adjacency matrix of the small undirected graph represented below. We see that $A$ is symmetric $n \times n$ matrix of $0$'s and $1$'s.
A=as_adj(g1, sparse = FALSE) A
ggplotadjacency(as_adj(g1, sparse = FALSE))
Here is how we plot a simple graph:
library("igraph") edges1 = matrix(c(1,3,2,3,3,4,4,5,4,6), byrow = TRUE, ncol = 2) g1 = graph_from_edgelist(edges1, directed = FALSE) plot(g1, vertex.size = 25, edge.width = 5, vertex.color = "coral")
Now add two lines that label the nodes:
library("igraph") edges1 = matrix(c(1,3,2,3,3,4,4,5,4,6), byrow = TRUE, ncol = 2)
library("igraph") edges1 = matrix(c(1,3,2,3,3,4,4,5,4,6), byrow = TRUE, ncol = 2) g1 = graph_from_edgelist(edges1, directed = FALSE) vertex_attr(g1, name = "vname") = paste0(1:6) plot(g1, vertex.size = 25, edge.width = 5, vertex.color = "coral")
Can you think of an alternative way of reading the graph from an edge set dataframe (the code should be two or three lines)?
edges=_____ sg = _____
edges = "1,3\n2,3\n3,4\n4,6\n4,5" sg = graph_from_data_frame(read.csv(textConnection(edges), header = FALSE), directed = FALSE) sg
The nodes or vertices which are the colored circles with numbers in them in the Figure.
Edges or connections, the segments that join the nodes and which can be directed or not.
Edge lengths, when not specified, we suppose they are all one and compute the distance between vertices on the graph as the number the edges traversed. On the other hand, in many situations we have meaningful edge lengths or strengths of connections between vertices that we can use both in the plots and analyses.
We call a weighted, directed graph a network. Networks have adjacency matrices $A$ which are $n$ by $n$ matrices of positive numbers corresponding to the edge lengths.
Edge and node attributes: optionally, each edge or each node can be mapped to further continuous or categorical variables.
As a starting example, the following data came from the STRING database, it is a chemokine network formatted as a txt
file.
Look at the file linked here
We read the file in as a simple table for the time being.
#Read the data from the data website #chemodats =read.table("https://web.stanford.edu/class/bios221/data/small_chemokine.txt",header=TRUE) data(chemodats)
head(chemodats[,1:8])
Question: What does each row of chemodats
correspond to?
Our first function is graph_from_data_frame
from the igraph
package:
gr = graph_from_data_frame(chemodats[,c("node1", "node2")], directed = FALSE) E(gr)$weight = 1 V(gr)$size = centralization.degree(gr)$res ## You can save the graph for later use as an R object ## saveRDS(gr,file="../gr.RDS")
We created an annotation variable for the nodes. This was computed a the centralization index of the nodes with regards to this graph (larger nodes are more central to the graph).
Now we have an igraph
object, how would you plot it?
____(gr)
plot(gr)
Question: What would you type in order to
find out which are the default plotting parameters for this inherited plot of an igraph
object?
_____
?plot.igraph
This plot
function is the equivalent of a simple base R plot for gr
which is an object of type igraph
.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.