网络图简介
网络图表示着一群事物之间的关系,如果两个事物有关系,则对应结点之间有边。
实战
官方实例
所需的包
library(tidyverse)
library(viridis)
library(patchwork)
library(hrbrthemes)
# install.packages("ggraph")
library(ggraph)
library(igraph)
# install.packages("networkD3")
library(networkD3)
数据含义
这个数据表示如果两个人至少在同一出版物上列出过一次,则通过链接将他们联系起来。
数据读取
# Load researcher data
dataUU <- read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/13_AdjacencyUndirectedUnweighted.csv", header=TRUE)
这个数据的特点就是:
- 第一列是结点的名字;
- 如果两个结点右边,则对应矩阵里面的值为1,反之为
NA
。
数据处理
这个数据处理都是通用的,只要将我们的数据转换成dataUU
的格式即可。
# Transform the adjacency matrix in a long format
connect <- dataUU %>%
gather(key="to", value="value", -1) %>%
na.omit()
# Number of connection per person
c( as.character(connect$from), as.character(connect$to)) %>%
as.tibble() %>%
group_by(value) %>%
summarize(n=n()) -> coauth
colnames(coauth) <- c("name", "n")
# NetworkD3 format
graph=simpleNetwork(connect)
画图
# Plot
simpleNetwork(connect,
Source = 1, # column number of source
Target = 2, # column number of target
height = 880, # height of frame area in pixels
width = 1980,
linkDistance = 10, # distance between node. Increase this value to have more space between nodes
charge = -4, # numeric value indicating either the strength of the node repulsion (negative value) or attraction (positive value)
fontSize = 5, # size of the node names
fontFamily = "serif", # font og node names
linkColour = "#666", # colour of edges, MUST be a common colour for the whole graph
nodeColour = "#69b3a2", # colour of nodes, MUST be a common colour for the whole graph
opacity = 0.9, # opacity of nodes. 0=transparent. 1=no transparency
zoom = T # Can you zoom on the figure?
)
R语言画网络图结构
自己的例子
数据
名字 | 张三 | 李四 | 王五 | 赵六 | 冯七 | 蒋八 |
---|---|---|---|---|---|---|
张三 | 1 | 1 | 1 | 1 | ||
李四 | 1 | 1 | ||||
王五 | 1 | 1 | 1 | 1 | ||
赵六 | 1 | 1 | 1 | 1 | ||
冯七 | 1 | 1 | 1 | |||
蒋八 | 1 | 1 | 1 | |||
数据读取和处理都和前面一样。 |
# Libraries
library(tidyverse)
library(viridis)
library(patchwork)
library(hrbrthemes)
# install.packages("ggraph")
library(ggraph)
library(igraph)
# install.packages("networkD3")
library(networkD3)
# dataUU <- read.csv("D:/每日一图/关系网 - 副本.CSV")
dataUU <- read.csv("D:/每日一图/关系网.CSV")
# Transform the adjacency matrix in a long format
connect <- dataUU %>%
gather(key="to", value="value", -1) %>%
na.omit()
# Number of connection per person
c( as.character(connect$from), as.character(connect$to)) %>%
as.tibble() %>%
group_by(value) %>%
summarize(n=n()) -> coauth
colnames(coauth) <- c("name", "n")
# NetworkD3 format
graph=simpleNetwork(connect)
画图
画图的代码稍微改了一下linkDistance
和fontSize
参数,分别把节点间距离变长,字体改一下
# Plot
simpleNetwork(connect,
Source = 1, # column number of source
Target = 2, # column number of target
# height = 80, # height of frame area in pixels
# width = 180,
linkDistance = 38, # distance between node. Increase this value to have more space between nodes
charge = -4, # numeric value indicating either the strength of the node repulsion (negative value) or attraction (positive value)
fontSize = 5, # size of the node names
fontFamily = "serif", # font og node names
linkColour = "#666", # colour of edges, MUST be a common colour for the whole graph
nodeColour = "#69b3a2", # colour of nodes, MUST be a common colour for the whole graph
opacity = 0.9, # opacity of nodes. 0=transparent. 1=no transparency
zoom = T # Can you zoom on the figure?
)
最后得到下图
后记
网络在生信里面占据了很大作用,之后构建的网络可以通过这个方法可视化。
平常的数据,第一列一般不是结点名称,结点名称一般在行名上,这个时候我们可以用下面的代码进行处理一下,就变成我们想要的形式了
dataUU <- data.frame(rownames(dataUU),dataUU)