通过使用预定的XY坐标并使用相关度作为R中的边缘颜色来制作网络
Making a network by using predetermined XY coordinates and using degree of correlation as edge colors in R
我有这个相关矩阵:
> head(cmat)
1 2 3 4 5 6 7 8 9
1 1.0000000 0.3811486 0.4635226 0.4388138 0.4702924 0.3839215 0.3952252 0.3933645 0.4020303
2 0.3811486 1.0000000 0.4636466 0.3449465 0.4185577 0.4400996 0.3995343 0.4683042 0.4534727
3 0.4635226 0.4636466 1.0000000 0.3665173 0.5041320 0.4763060 0.4090055 0.4126498 0.3903248
4 0.4388138 0.3449465 0.3665173 1.0000000 0.4449320 0.4125759 0.4388138 0.3030503 0.3493223
10 11 12 13 14 15 16 17 18
1 0.4839340 0.4947885 0.4633059 0.4290341 0.4504393 0.4647089 0.4816216 0.4294152 0.4666731
2 0.4045505 0.4036112 0.4377636 0.3849775 0.3769241 0.4767528 0.4546915 0.4036112 0.3467831
3 0.5536620 0.4321896 0.4743869 0.5002220 0.4836144 0.5319749 0.4907812 0.4631280 0.3625720
4 0.4323536 0.4244193 0.4486245 0.4213758 0.4462888 0.4425709 0.4692482 0.3726047 0.3670268
19 20
1 0.3664227 0.4027118
2 0.3041602 0.3468899
3 0.4152681 0.3361121
4 0.2086564 0.3833584
我也有这 20 个项目中每一个的坐标作为数据框:
> head(coordinates)
X Y
1 19.908 250.861
2 6.767 253.552
3 18.280 264.838
4 31.000 263.078
5 42.900 271.389
6 54.495 269.625
我想做的是将这些坐标绘制为点,如下所示:
ggplot(coordinates, aes(X, Y))+
geom_point()
然后在每个点之间绘制连接线,其中线的颜色或粗细对应于相关矩阵中的相对值 cmat
。
我查看了 igraph
包,但我只设法显示带有自定义坐标的网络:
g <- graph.data.frame(cmat)
l <- as.matrix(coordinates)
plot(g,layout=l,rescale=T,axes=TRUE)
它还会将相关值分配给每个单元格。
有什么办法吗?
很难适用于您的确切案例,因为我们缺少您的确切数据。假设你想对 mtcars
数据集做类似的事情,我们可以使用 ggraph
包来更容易地绘制网络图。
我们假设数据集的 mpg
和 wt
变量是 XY 坐标。
library(ggplot2)
library(igraph)
library(ggraph)
data <- as.matrix(mtcars)
cor <- cor(t(apply(data, 2, scale)))
graph <- graph.adjacency(cor, weighted = TRUE)
ggraph(graph, layout = data[, c("mpg", "wt")]) +
geom_edge_link(aes(colour = weight)) +
geom_node_point() +
scale_edge_color_gradient2()
由 reprex package (v0.3.0)
于 2021 年 1 月 3 日创建
如果我们想在 vanilla ggplot2 中做类似的事情,您必须手动构建边。
cor_df <- reshape2::melt(cor)
cor_df <- transform(
cor_df,
x = data[Var1, "mpg"],
xend = data[Var2, "mpg"],
y = data[Var1, "wt"],
yend = data[Var2, "wt"]
)
ggplot(mtcars) +
geom_segment(aes(x, y, xend = xend, yend = yend, colour = value),
data = cor_df) +
geom_point(aes(mpg, wt)) +
scale_colour_gradient2()
我有这个相关矩阵:
> head(cmat)
1 2 3 4 5 6 7 8 9
1 1.0000000 0.3811486 0.4635226 0.4388138 0.4702924 0.3839215 0.3952252 0.3933645 0.4020303
2 0.3811486 1.0000000 0.4636466 0.3449465 0.4185577 0.4400996 0.3995343 0.4683042 0.4534727
3 0.4635226 0.4636466 1.0000000 0.3665173 0.5041320 0.4763060 0.4090055 0.4126498 0.3903248
4 0.4388138 0.3449465 0.3665173 1.0000000 0.4449320 0.4125759 0.4388138 0.3030503 0.3493223
10 11 12 13 14 15 16 17 18
1 0.4839340 0.4947885 0.4633059 0.4290341 0.4504393 0.4647089 0.4816216 0.4294152 0.4666731
2 0.4045505 0.4036112 0.4377636 0.3849775 0.3769241 0.4767528 0.4546915 0.4036112 0.3467831
3 0.5536620 0.4321896 0.4743869 0.5002220 0.4836144 0.5319749 0.4907812 0.4631280 0.3625720
4 0.4323536 0.4244193 0.4486245 0.4213758 0.4462888 0.4425709 0.4692482 0.3726047 0.3670268
19 20
1 0.3664227 0.4027118
2 0.3041602 0.3468899
3 0.4152681 0.3361121
4 0.2086564 0.3833584
我也有这 20 个项目中每一个的坐标作为数据框:
> head(coordinates)
X Y
1 19.908 250.861
2 6.767 253.552
3 18.280 264.838
4 31.000 263.078
5 42.900 271.389
6 54.495 269.625
我想做的是将这些坐标绘制为点,如下所示:
ggplot(coordinates, aes(X, Y))+
geom_point()
然后在每个点之间绘制连接线,其中线的颜色或粗细对应于相关矩阵中的相对值 cmat
。
我查看了 igraph
包,但我只设法显示带有自定义坐标的网络:
g <- graph.data.frame(cmat)
l <- as.matrix(coordinates)
plot(g,layout=l,rescale=T,axes=TRUE)
它还会将相关值分配给每个单元格。
有什么办法吗?
很难适用于您的确切案例,因为我们缺少您的确切数据。假设你想对 mtcars
数据集做类似的事情,我们可以使用 ggraph
包来更容易地绘制网络图。
我们假设数据集的 mpg
和 wt
变量是 XY 坐标。
library(ggplot2)
library(igraph)
library(ggraph)
data <- as.matrix(mtcars)
cor <- cor(t(apply(data, 2, scale)))
graph <- graph.adjacency(cor, weighted = TRUE)
ggraph(graph, layout = data[, c("mpg", "wt")]) +
geom_edge_link(aes(colour = weight)) +
geom_node_point() +
scale_edge_color_gradient2()
由 reprex package (v0.3.0)
于 2021 年 1 月 3 日创建如果我们想在 vanilla ggplot2 中做类似的事情,您必须手动构建边。
cor_df <- reshape2::melt(cor)
cor_df <- transform(
cor_df,
x = data[Var1, "mpg"],
xend = data[Var2, "mpg"],
y = data[Var1, "wt"],
yend = data[Var2, "wt"]
)
ggplot(mtcars) +
geom_segment(aes(x, y, xend = xend, yend = yend, colour = value),
data = cor_df) +
geom_point(aes(mpg, wt)) +
scale_colour_gradient2()