一、三个R包的比较
二、示例数据分析
2.1 安装和加载必要的包
# 安装包
#install.packages("GGally")
#install.packages("geomnet")
#install.packages("ggnetwork")
# 加载包
library("GGally")
library("geomnet")
library("ggnetwork")
library("statnet")
2.2 加载数据
# 加载数据
data("football", package = "geomnet")
rownames(football$vertices) <- football$vertices$label
2.3 创建网络
# 从边列表创建网络
fb.net = network::network(football$edges[,1:2])
2.4 添加顶点和边的属性
# 添加顶点属性:队伍所在的会议
fb.net %v% "conf" <- football$vertices[network.vertex.names(fb.net), "value"]
# 添加边属性:两队是否同属一个会议
set.edge.attribute(fb.net, "same.conf", football$edges$same.conf)
set.edge.attribute(fb.net, "lty", ifelse(fb.net %e% "same.conf" == 1, 1, 2))
三、ggnet2
3.1 功能特点
3.2 问题
3.3 示例代码
set.seed(3212019)
pggnet2 = ggnet2(fb.net, # 输入 `network` 对象
mode = "fruchtermanreingold", # 来自 `network` 包的布局
layout.par = list(cell.jitter=0.75), # 可以传递布局参数
# 节点属性
node.color = "conf",
palette = "Paired", # 颜色板 palette="Set3",
node.size = 5,
# node.size = "degree",
# size.cut = 3, # 使用分位数将大小切割为三个类别
# size = "conf",
# 手动映射大小:size.palette = c("Atlantic Coast" = 1,...),
# node.shape = "conf",
node.alpha = 0.5,
# node.label = TRUE,
# 边缘
edge.color = c("color", "grey50"), # 第一个值:同一组的节点使用相同颜色,否则使用第二个参数
edge.alpha = 0.5,
edge.size = 0.3,
edge.lty = "lty",
# edge.label = 1,
# edge.label.size = 1,
# 图例
color.legend = "Conference",
# legend.size = 10,
# legend.position = "bottom"
) +
geom_point(aes(color = color), size = 3) # 可以像ggplot对象一样处理并添加geom_xx层
pggnet2
## 将其作为数据框处理以添加geom_xx层
pggnet2$data %>% names()
## [1] "label" "alpha" "color" "shape" "size" "x" "y"

四、geomnet
4.1 功能特点
4.2 问题
4.3 示例代码
# 合并顶点和边
ver.conf = football$vertices %>% mutate(from = label) %>% select(-label)
fb.df = left_join(football$edges, ver.conf, by = "from")
# 创建数据图
set.seed(3212019)
pgeomnet =
ggplot(data = fb.df, # 输入:数据框
aes(from_id = from, to_id = to)) +
geom_net(layout.alg = 'fruchtermanreingold',
aes(colour = value, group = value,
linetype = factor(same.conf != 1)),
linewidth = 0.5,
size = 5, vjust = -0.75, alpha = 1) +
theme_net() +
# theme(legend.position = "bottom") +
scale_colour_brewer("Conference", palette = "Paired") +
guides(linetype = FALSE)
pgeomnet

五、ggnetwork
5.1 特点
5.2 问题
5.3 示例代码
## 需要先安装 intergraph 包用于处理 igraph 对象
#install.packages("intergraph")
library("intergraph")
## 创建 igraph 对象
fb.igra = graph_from_data_frame(football$edges[,1:2], directed = FALSE)
V(fb.igra)$conf = football$vertices[V(fb.igra)$name, "value"]
E(fb.igra)$same.conf = football$edges$same.conf
E(fb.igra)$lty = ifelse(E(fb.igra)$same.conf == 1, 1, 2)
## 设置种子
set.seed(3212019)
## 使用 ggnetwork 和 ggplot 绘图
pggnetwork =
ggplot(
ggnetwork( # 提供底层数据框
fb.igra, # 输入:网络对象
layout = "fruchtermanreingold", # 布局
cell.jitter = 0.75
),
aes(x, y, xend = xend, yend = yend)
) +
geom_edges(
aes(linetype = as.factor(same.conf)),
color = "grey50",
curvature = 0.2,
alpha = 0.5
) +
geom_nodes(
aes(color = conf),
size = 5,
alpha = 0.5
) +
scale_color_brewer("Conference", palette = "Paired") +
scale_linetype_manual(values = c(2, 1)) +
guides(linetype = FALSE) +
theme_blank() +
geom_nodes(
aes(color = conf),
size = 3
) # 可以像 ggplot 对象一样处理并添加 geom_xx 层
pggnetwork

六、ggnet2、geomnet、ggnetwork 的扩展
6.1 ggplot2 + plotly
6.2 加载 plotly 库
library("plotly")
6.3 将 ggplot2 对象转换为 plotly 对象
ggplotly(pggnet2 + coord_fixed()) %>% hide_guides()
ggplotly(pgeomnet + coord_fixed()) %>% hide_guides()

# ggplotly(pggnetwork + coord_fixed()) %>% hide_guides()

6.4 创建新的网络图 pggnetwork2
pggnetwork2 =
ggplot(
ggnetwork( # 提供底层数据框
fb.igra, # 输入:网络对象
layout = "fruchtermanreingold", # 布局
cell.jitter = 0.75
),
aes(x, y, xend = xend, yend = yend)
) + # 边的映射
geom_edges(
aes(linetype = as.factor(same.conf)),
color = "grey50",
alpha = 0.5
) +
geom_nodes(
aes(color = conf),
size = 5,
alpha = 0.5
) +
scale_color_brewer("Conference", palette = "Paired") +
scale_linetype_manual(values = c(2, 1)) +
guides(linetype = FALSE) +
theme_blank() +
geom_nodes(
aes(color = conf),
size = 3
)
ggplotly(pggnetwork2 + coord_fixed()) %>% hide_guides()

七、分面动态网络
7.1 创建网络
# 查看电子邮件数据集的边和节点的属性名
names(email$edges)
## [1] "From" "eID" "Date" "Subject" "to"
## [6] "month" "day" "year" "nrecipients"
names(email$nodes)
## [1] "label" "LastName"
## [3] "FirstName" "BirthDate"
## [5] "BirthCountry" "Gender"
## [7] "CitizenshipCountry" "CitizenshipBasis"
## [9] "CitizenshipStartDate" "PassportCountry"
## [11] "PassportIssueDate" "PassportExpirationDate"
## [13] "CurrentEmploymentType" "CurrentEmploymentTitle"
## [15] "CurrentEmploymentStartDate" "MilitaryServiceBranch"
## [17] "MilitaryDischargeType" "MilitaryDischargeDate"
# 从电子邮件数据集中提取边列表:移除发送给所有员工的电子邮件
edges = email$edges %>% filter(nrecipients < 54) %>% select(From, to, day)
# 创建网络对象
em.net <- network(edges[, 1:2])
# 分配边的属性(天)
set.edge.attribute(em.net, "day", edges[, 3])
# 分配节点的属性(员工类型)
em.cet <- as.character(email$nodes$CurrentEmploymentType)
names(em.cet) = email$nodes$label
em.net %v% "curr_empl_type" <- em.cet[network.vertex.names(em.net)]
# 设置种子以确保可重复性
set.seed(3212019)
# 使用 ggnetwork 创建可视化
ggplot(
ggnetwork(
em.net,
arrow.gap = 0.02, # 箭头间隙
by = "day", # 按天分面
layout = "kamadakawai" # 布局算法
),
aes(x, y, xend = xend, yend = yend)
) +
geom_edges(
aes(color = curr_empl_type),
alpha = 0.25,
arrow = arrow(length = unit(5, "pt"), type = "closed") # 定义箭头
) +
geom_nodes(aes(color = curr_empl_type), size = 1.5) + # 定义节点
scale_color_brewer("Employment Type", palette = "Set1") + # 颜色映射
facet_wrap(. ~ day, nrow = 2, labeller = "label_both") + # 分面显示
theme_facet(legend.position = "bottom") # 调整主题

参考资料