网络其实就是一张矩阵-ggplot可视化矩阵(热图)
ggplot可视化矩阵
可视化谱图案例
library(dplyr)
library(ggplot2)
# The most common use for rectangles is to draw a surface. You always want
# to use geom_raster here because it's so much faster, and produces
# smaller output when saving to PDF
ggplot(faithfuld, aes(waiting, eruptions)) +
geom_raster(aes(fill = density))
# Interpolation smooths the surface & is most helpful when rendering images.
ggplot(faithfuld, aes(waiting, eruptions)) +
geom_raster(aes(fill = density), interpolate = TRUE)
ggplot绘制热图函数geom_rect
# If you want to draw arbitrary rectangles, use geom_tile() or geom_rect()
df <- data.frame(
x = rep(c(2, 5, 7, 9, 12), 2),
y = rep(c(1, 2), each = 5),
z = factor(rep(1:5, each = 2)),
w = rep(diff(c(0, 4, 6, 8, 10, 14)), 2)
)
ggplot(df, aes(x, y)) +
geom_tile(aes(fill = z), colour = "grey50")
ggplot(df, aes(x, y, width = w)) +
geom_tile(aes(fill = z), colour = "grey50")
ggplot(df, aes(xmin = x - w / 2, xmax = x + w / 2, ymin = y, ymax = y + 1)) +
geom_rect(aes(fill = z), colour = "grey50")
ggplot 版本热图函数geom_raster
# Justification controls where the cells are anchored
df <- expand.grid(x = 0:5, y = 0:5)
df$z <- runif(nrow(df))
# default is compatible with geom_tile()
ggplot(df, aes(x, y, fill = z)) + geom_raster()
# zero padding
ggplot(df, aes(x, y, fill = z)) + geom_raster(hjust = 0, vjust = 0)
library(igraph)
# 读入边文件和点文件-注意这两个文件格式
original_edgelist <- read.csv("./adjacency_plot-master//data/csv/goltzius_edges.csv", stringsAsFactors = FALSE)
original_nodelist <- read.csv("./adjacency_plot-master//data/csv/goltzius_nodes.csv", stringsAsFactors = FALSE)
head(original_edgelist)## from to weight
## 1 Jan van der Straet Philips Galle 494
## 2 Maerten de Vos Philips Galle 80
## 3 Johannes Wierix Philips Galle 37
## 4 Theodoor Galle Philips Galle 91
## 5 Hendrik Goltzius Philips Galle 43
## 6 Crispijn de Passe the Elder Philips Galle 43head(original_nodelist)
## name birth nationality
## 1 Philips Galle 1537 Flemish
## 2 Jan van der Straet 1523 Flemish
## 3 Raphael Sadeler I 1560 Flemish
## 4 Maerten de Vos 1532 Flemish
## 5 Albrecht D眉rer 1471 German
## 6 Johannes Wierix 1539 Flemish# 构建igraph对象
graph <- graph.data.frame(original_edgelist, directed = TRUE, vertices = original_nodelist)
# 增加网络中节点属性,添加到节点列表中
V(graph)$comm <- membership(optimal.community(graph))
V(graph)$degree <- degree(graph)
V(graph)$closeness <- centralization.closeness(graph)$res
V(graph)$betweenness <- centralization.betweenness(graph)$res
V(graph)$eigen <- centralization.evcent(graph)$vector# 重新得到节点和边的表格,这个时候就更加丰富了
node_list <- get.data.frame(graph, what = "vertices")
edge_list <- get.data.frame(graph, what = "edges") %>%
inner_join(node_list %>% select(name, comm), by = c("from" = "name")) %>%
inner_join(node_list %>% select(name, comm), by = c("to" = "name")) %>%
mutate(group = ifelse(comm.x == comm.y, comm.x, NA) %>% factor())
使用geom_tile函数 热图绘制
# 节点
all_nodes <- sort(node_list$name)
# Adjust the 'to' and 'from' factor levels so they are equal
# to this complete list of node names
plot_data <- edge_list %>% mutate(
to = factor(to, levels = all_nodes),
from = factor(from, levels = all_nodes))
head(plot_data)## from to weight comm.x comm.y group
## 1 Jan van der Straet Philips Galle 494 1 1 1
## 2 Maerten de Vos Philips Galle 80 2 1 <NA>
## 3 Johannes Wierix Philips Galle 37 1 1 1
## 4 Theodoor Galle Philips Galle 91 1 1 1
## 5 Hendrik Goltzius Philips Galle 43 4 1 <NA>
## 6 Crispijn de Passe the Elder Philips Galle 43 2 1 <NA># ?geom_raster
# Create the adjacency matrix plot
ggplot(plot_data, aes(x = from, y = to, fill = group)) +
geom_tile() +
theme_bw() +
# Because we need the x and y axis to display every node,
# not just the nodes that have connections to each other,
# make sure that ggplot does not drop unused factor levels
scale_x_discrete(drop = FALSE) +
scale_y_discrete(drop = FALSE) +
theme(
# Rotate the x-axis lables so they are legible
axis.text.x = element_text(angle = 270, hjust = 0),
# Force the plot into a square aspect ratio
aspect.ratio = 1,
# 去除图例
legend.position = "none")
# Create a character vector of node names sorted by their
# community membership. Here, I rearrange the node_list
# table by the "comm" variable, then extract the
# "name" vector
name_order <- (node_list %>% arrange(comm))$name
# Reorder edge_list "from" and "to" factor levels based on
# this new name_order
plot_data <- edge_list %>% mutate(
to = factor(to, levels = name_order),
from = factor(from, levels = name_order))ggplot(plot_data, aes(x = from, y = to, fill = group)) +
geom_raster() +
theme_bw() +
# Because we need the x and y axis to display every node,
# not just the nodes that have connections to each other,
# make sure that ggplot does not drop unused factor levels
scale_x_discrete(drop = FALSE) +
scale_y_discrete(drop = FALSE) +
theme(
# Rotate the x-axis lables so they are legible
axis.text.x = element_text(angle = 270, hjust = 0),
# Force the plot into a square aspect ratio
aspect.ratio = 1,
# Hide the legend (optional)
legend.position = "none")