本期教程:网络可视化。 应用场景:蛋白互作网络(
PPI);ceRNA网络;社交关系图;相关性图等。 需要的文件:我们在使用network,igraph等包进行网络可视化的时候,一般需要两个数据,nodes数据和edges,即节点数据和边数据。ヽ( o・ェ・)ノ
rm(list = ls())
library(tidyverse)
library(network)
library(igraph)
本次使用的示例数据是Daniel van der Meulen在1585年收到的信件所组成,包括writer,source, destination和date
letters <- read_csv("correspondence-data-1585.csv")

我们将source和destination提取出来并去重,整理为nodes文件;同时,我们为每一个城市创建一个ID.
sources <- letters %>%
distinct(source) %>%
rename(label = source)
destinations <- letters %>%
distinct(destination) %>%
rename(label = destination)
nodes <- full_join(sources, destinations, by = "label")%>%
rowid_to_column("id")

整理edges文件与nodes文件类似;
在此,我们计算一下从source城市到destination城市间的来信次数,定义为weight;
后面我们会以weight定义边的粗细;
最后我们将nodes文件中的ID加入。
edges <- letters %>%
group_by(source, destination) %>%
summarise(weight = n()) %>%
ungroup() %>%
left_join(nodes, by = c("source" = "label")) %>%
rename(from = id) %>%
left_join(nodes, by = c("destination" = "label")) %>%
rename(to = id)
edges <- edges %>%
dplyr::select(., from, to, weight)

方法一:
network包可视化
routes_network <- network(edges,
nodes,
matrix.type = "edgelist", # "adjacency",
# "edgelist",
# "incidence",
ignore.eval = FALSE)
note! matrix.type有三个选项,分别为adjacency, edgelist, incidence; 这里我们是edgelist的格式,有时你可能会有adjacency格式的数据做为输入文件.
plot(routes_network, vertex.cex = 3)

plot(routes_network, vertex.cex = 3, mode = "circle")

方法二:
igraph包可视化
detach(package:network)
rm(routes_network)
这里用到
igraph包的graph_from_data_frame函数
routes_igraph <- graph_from_data_frame(d = edges, vertices = nodes, directed = TRUE)
plot(routes_igraph, edge.arrow.size = 0.2)

这里采用
graphopt算法进行排列,可以更直观地看到Haarlem, Antwerp和Delft之间的关系
plot(routes_igraph,
layout = layout_with_graphopt,
edge.arrow.size = 0.2)

✅
add_layout_(); ✅component_wise(); ✅layout_as_bipartite(); ✅layout_as_star(); ✅layout_as_tree(); ✅layout_in_circle(); ✅layout_nicely(); ✅layout_on_grid(); ✅layout_on_sphere(); ✅layout_randomly(); ✅layout_with_dh(); ✅layout_with_fr(); ✅layout_with_gem(); ✅layout_with_graphopt(); ✅layout_with_kk(); ✅layout_with_lgl(); ✅layout_with_mds(); ✅layout_with_sugiyama(); ✅merge_coords(); ✅norm_coords(); ✅normalize()


甜甜圈最后祝大家早日不卷!~