节点使用图形和数据框跨模块链接
Node links across modules with graph and dataframe
我有一个图形对象(加权网络),其中节点属于数据帧中定义的不同模块化 类。我想计算一个节点到所有不同模块(它自己的模块和每个其他模块)中其他节点的链接数。
如何根据我的图形对象 g
和数据框 modules
编写此计算?。我的预期输出是一个数据框,其中包含每个节点(国家/地区)到模块 1、模块 2、模块 3 等的链接。感谢任何帮助!
可重现的例子:
g <- structure(c(32, 12, 54, 0, 0, 0, 73, 0, 91, 0, 0, 65.27657092, 99,
76, 0, 0, 0, 36.95395031, 0, 88, 44, 0, 0, 86.09277176, 0, 0, 0,
84, 11, 0, 0, 0, 0, 0, 45, 0), .Dim = c(6L, 6L), .Dimnames = list(
c("Indonesia", "Iran (Islamic Republic of)", "Iraq", "Ireland",
"Israel", "Italy"), c("Indonesia", "Iran..Islamic.Republic.of.",
"Iraq", "Ireland", "Israel", "Italy")))
library(igraph)
g <- graph_from_adjacency_matrix(g)
modules <- structure(list(Label = structure(73:78, .Label = c("Afghanistan",
"Albania", "Algeria", "Angola", "Antigua and Barbuda", "Argentina",
"Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahrain",
"Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin",
"Bhutan", "Bolivia (Plurinational State of)", "Bosnia and Herzegovina",
"Botswana", "Brazil", "Brunei Darussalam", "Bulgaria", "Burkina Faso",
"Burundi", "C?te d'Ivoire", "Cambodia", "Cameroon", "Canada",
"Central African Republic", "Chile", "China", "China, Hong Kong SAR",
"China, Macao SAR", "China, Taiwan Province of", "Colombia",
"Congo", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czechia",
"Democratic People's Republic of Korea", "Democratic Republic of the Congo",
"Denmark", "Dominican Republic", "Ecuador", "Egypt", "El Salvador",
"Eritrea", "Estonia", "Eswatini", "Ethiopia", "Finland", "France",
"Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Greece", "Grenada",
"Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Honduras",
"Hungary", "India", "Indonesia", "Iran (Islamic Republic of)",
"Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan",
"Kazakhstan", "Kenya", "Kuwait", "Kyrgyzstan", "Lao People's Democratic Republic",
"Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Lithuania",
"Luxembourg", "Madagascar", "Malawi", "Malaysia", "Mali", "Malta",
"Mauritania", "Mexico", "Mongolia", "Montenegro", "Morocco",
"Mozambique", "Myanmar", "Namibia", "Nepal", "Netherlands", "New Zealand",
"Nicaragua", "Niger", "Nigeria", "North Macedonia", "Norway",
"Oman", "Pakistan", "Palestine", "Panama", "Papua New Guinea",
"Paraguay", "Peru", "Philippines", "Poland", "Portugal", "Qatar",
"Republic of Korea", "Republic of Moldova", "Romania", "Russian Federation",
"Rwanda", "Saint Kitts and Nevis", "Saint Lucia", "Saint Vincent and the Grenadines",
"Saudi Arabia", "Senegal", "Serbia", "Sierra Leone", "Singapore",
"Slovakia", "Slovenia", "Somalia", "South Africa", "Spain", "Sri Lanka",
"Sudan", "Suriname", "Sweden", "Switzerland", "Syrian Arab Republic",
"Tajikistan", "Thailand", "Timor-Leste", "Trinidad and Tobago",
"Tunisia", "Turkey", "Turkmenistan", "Uganda", "Ukraine", "United Arab Emirates",
"United Kingdom of Great Britain and Northern Ireland", "United Republic of Tanzania",
"United States of America", "Uruguay", "Uzbekistan", "Venezuela (Bolivarian Republic of)",
"Viet Nam", "Yemen", "Zambia", "Zimbabwe"), class = "factor"),
modularity_class = c(0L, 3L, 2L, 1L, 4L, 4L)), row.names = c("Indonesia",
"Iran (Islamic Republic of)", "Iraq", "Ireland", "Israel", "Italy"
), class = "data.frame")
更新 2
如果你想要有重复边的无向图,你可以试试这个代码
g <- graph_from_adjacency_matrix(+(df > 0), diag = FALSE) %>%
as.undirected() %>%
set_vertex_attr(
name = "module",
value = with(modules, modularity_class[match(names(V(.)), Label)])
)
out <- sapply(V(g), function(x) {
m <- neighbors(g, x)$module
table(m[m != V(g)[x]$module])
})
这样
> out
$Indonesia
2 3
1 1
$Iran
0 1 2 4
1 1 1 1
$Iraq
0 1 3 4
1 1 1 1
$Ireland
2 3 4
1 1 2
$Israel
1
1
$Italy
1 2 3
1 1 1
更新
如果要查看分布情况,可以使用table
sapply(V(g), function(x) {
m <- neighbors(g, x)$module
table(m[m != V(g)[x]$module])
})
这给出了
$Indonesia
2 3
99 73
$Iran
0 1 2
12 88 76
$Iraq
0 1 3
54 44 91
$Ireland
4
84
$Israel
< table of extent 0 >
$Italy
1 2 3
86 36 65
我们可以使用set_vertex_attr
在modules
中使用modularity_class
设置顶点属性
g <- g %>%
set_vertex_attr(
name = "module",
value = with(modules, modularity_class[match(names(V(.)), Label)])
)
然后我们找到每个顶点的 neighbors
并总结具有不同模块化的邻居的计数 class
out <- sapply(V(g), function(x) sum(neighbors(g, x)$module != V(g)[x]$module))[x]$module))
这给出了
> out
Indonesia Iran Iraq Ireland Israel Italy
172 176 189 84 0 187
数据
df <- structure(c(
32, 12, 54, 0, 0, 0, 73, 0, 91, 0, 0, 65.27657092, 99,
76, 0, 0, 0, 36.95395031, 0, 88, 44, 0, 0, 86.09277176, 0, 0, 0,
84, 11, 0, 0, 0, 0, 0, 45, 0
), .Dim = c(6L, 6L), .Dimnames = list(
c(
"Indonesia", "Iran", "Iraq", "Ireland",
"Israel", "Italy"
), c(
"Indonesia", "Iran",
"Iraq", "Ireland", "Israel", "Italy"
)
))
modules <- structure(list(
Label = structure(73:78, .Label = c(
"Afghanistan",
"Albania", "Algeria", "Angola", "Antigua and Barbuda", "Argentina",
"Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahrain",
"Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin",
"Bhutan", "Bolivia (Plurinational State of)", "Bosnia and Herzegovina",
"Botswana", "Brazil", "Brunei Darussalam", "Bulgaria", "Burkina Faso",
"Burundi", "C?te d'Ivoire", "Cambodia", "Cameroon", "Canada",
"Central African Republic", "Chile", "China", "China, Hong Kong SAR",
"China, Macao SAR", "China, Taiwan Province of", "Colombia",
"Congo", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czechia",
"Democratic People's Republic of Korea", "Democratic Republic of the Congo",
"Denmark", "Dominican Republic", "Ecuador", "Egypt", "El Salvador",
"Eritrea", "Estonia", "Eswatini", "Ethiopia", "Finland", "France",
"Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Greece", "Grenada",
"Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Honduras",
"Hungary", "India", "Indonesia", "Iran",
"Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan",
"Kazakhstan", "Kenya", "Kuwait", "Kyrgyzstan", "Lao People's Democratic Republic",
"Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Lithuania",
"Luxembourg", "Madagascar", "Malawi", "Malaysia", "Mali", "Malta",
"Mauritania", "Mexico", "Mongolia", "Montenegro", "Morocco",
"Mozambique", "Myanmar", "Namibia", "Nepal", "Netherlands", "New Zealand",
"Nicaragua", "Niger", "Nigeria", "North Macedonia", "Norway",
"Oman", "Pakistan", "Palestine", "Panama", "Papua New Guinea",
"Paraguay", "Peru", "Philippines", "Poland", "Portugal", "Qatar",
"Republic of Korea", "Republic of Moldova", "Romania", "Russian Federation",
"Rwanda", "Saint Kitts and Nevis", "Saint Lucia", "Saint Vincent and the Grenadines",
"Saudi Arabia", "Senegal", "Serbia", "Sierra Leone", "Singapore",
"Slovakia", "Slovenia", "Somalia", "South Africa", "Spain", "Sri Lanka",
"Sudan", "Suriname", "Sweden", "Switzerland", "Syrian Arab Republic",
"Tajikistan", "Thailand", "Timor-Leste", "Trinidad and Tobago",
"Tunisia", "Turkey", "Turkmenistan", "Uganda", "Ukraine", "United Arab Emirates",
"United Kingdom of Great Britain and Northern Ireland", "United Republic of Tanzania",
"United States of America", "Uruguay", "Uzbekistan", "Venezuela (Bolivarian Republic of)",
"Viet Nam", "Yemen", "Zambia", "Zimbabwe"
), class = "factor"),
modularity_class = c(0L, 3L, 2L, 1L, 4L, 4L)
), row.names = c(
"Indonesia",
"Iran", "Iraq", "Ireland", "Israel", "Italy"
), class = "data.frame")
我有一个图形对象(加权网络),其中节点属于数据帧中定义的不同模块化 类。我想计算一个节点到所有不同模块(它自己的模块和每个其他模块)中其他节点的链接数。
如何根据我的图形对象 g
和数据框 modules
编写此计算?。我的预期输出是一个数据框,其中包含每个节点(国家/地区)到模块 1、模块 2、模块 3 等的链接。感谢任何帮助!
可重现的例子:
g <- structure(c(32, 12, 54, 0, 0, 0, 73, 0, 91, 0, 0, 65.27657092, 99,
76, 0, 0, 0, 36.95395031, 0, 88, 44, 0, 0, 86.09277176, 0, 0, 0,
84, 11, 0, 0, 0, 0, 0, 45, 0), .Dim = c(6L, 6L), .Dimnames = list(
c("Indonesia", "Iran (Islamic Republic of)", "Iraq", "Ireland",
"Israel", "Italy"), c("Indonesia", "Iran..Islamic.Republic.of.",
"Iraq", "Ireland", "Israel", "Italy")))
library(igraph)
g <- graph_from_adjacency_matrix(g)
modules <- structure(list(Label = structure(73:78, .Label = c("Afghanistan",
"Albania", "Algeria", "Angola", "Antigua and Barbuda", "Argentina",
"Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahrain",
"Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin",
"Bhutan", "Bolivia (Plurinational State of)", "Bosnia and Herzegovina",
"Botswana", "Brazil", "Brunei Darussalam", "Bulgaria", "Burkina Faso",
"Burundi", "C?te d'Ivoire", "Cambodia", "Cameroon", "Canada",
"Central African Republic", "Chile", "China", "China, Hong Kong SAR",
"China, Macao SAR", "China, Taiwan Province of", "Colombia",
"Congo", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czechia",
"Democratic People's Republic of Korea", "Democratic Republic of the Congo",
"Denmark", "Dominican Republic", "Ecuador", "Egypt", "El Salvador",
"Eritrea", "Estonia", "Eswatini", "Ethiopia", "Finland", "France",
"Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Greece", "Grenada",
"Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Honduras",
"Hungary", "India", "Indonesia", "Iran (Islamic Republic of)",
"Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan",
"Kazakhstan", "Kenya", "Kuwait", "Kyrgyzstan", "Lao People's Democratic Republic",
"Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Lithuania",
"Luxembourg", "Madagascar", "Malawi", "Malaysia", "Mali", "Malta",
"Mauritania", "Mexico", "Mongolia", "Montenegro", "Morocco",
"Mozambique", "Myanmar", "Namibia", "Nepal", "Netherlands", "New Zealand",
"Nicaragua", "Niger", "Nigeria", "North Macedonia", "Norway",
"Oman", "Pakistan", "Palestine", "Panama", "Papua New Guinea",
"Paraguay", "Peru", "Philippines", "Poland", "Portugal", "Qatar",
"Republic of Korea", "Republic of Moldova", "Romania", "Russian Federation",
"Rwanda", "Saint Kitts and Nevis", "Saint Lucia", "Saint Vincent and the Grenadines",
"Saudi Arabia", "Senegal", "Serbia", "Sierra Leone", "Singapore",
"Slovakia", "Slovenia", "Somalia", "South Africa", "Spain", "Sri Lanka",
"Sudan", "Suriname", "Sweden", "Switzerland", "Syrian Arab Republic",
"Tajikistan", "Thailand", "Timor-Leste", "Trinidad and Tobago",
"Tunisia", "Turkey", "Turkmenistan", "Uganda", "Ukraine", "United Arab Emirates",
"United Kingdom of Great Britain and Northern Ireland", "United Republic of Tanzania",
"United States of America", "Uruguay", "Uzbekistan", "Venezuela (Bolivarian Republic of)",
"Viet Nam", "Yemen", "Zambia", "Zimbabwe"), class = "factor"),
modularity_class = c(0L, 3L, 2L, 1L, 4L, 4L)), row.names = c("Indonesia",
"Iran (Islamic Republic of)", "Iraq", "Ireland", "Israel", "Italy"
), class = "data.frame")
更新 2
如果你想要有重复边的无向图,你可以试试这个代码
g <- graph_from_adjacency_matrix(+(df > 0), diag = FALSE) %>%
as.undirected() %>%
set_vertex_attr(
name = "module",
value = with(modules, modularity_class[match(names(V(.)), Label)])
)
out <- sapply(V(g), function(x) {
m <- neighbors(g, x)$module
table(m[m != V(g)[x]$module])
})
这样
> out
$Indonesia
2 3
1 1
$Iran
0 1 2 4
1 1 1 1
$Iraq
0 1 3 4
1 1 1 1
$Ireland
2 3 4
1 1 2
$Israel
1
1
$Italy
1 2 3
1 1 1
更新
如果要查看分布情况,可以使用table
sapply(V(g), function(x) {
m <- neighbors(g, x)$module
table(m[m != V(g)[x]$module])
})
这给出了
$Indonesia
2 3
99 73
$Iran
0 1 2
12 88 76
$Iraq
0 1 3
54 44 91
$Ireland
4
84
$Israel
< table of extent 0 >
$Italy
1 2 3
86 36 65
我们可以使用set_vertex_attr
在modules
modularity_class
设置顶点属性
g <- g %>%
set_vertex_attr(
name = "module",
value = with(modules, modularity_class[match(names(V(.)), Label)])
)
然后我们找到每个顶点的 neighbors
并总结具有不同模块化的邻居的计数 class
out <- sapply(V(g), function(x) sum(neighbors(g, x)$module != V(g)[x]$module))[x]$module))
这给出了
> out
Indonesia Iran Iraq Ireland Israel Italy
172 176 189 84 0 187
数据
df <- structure(c(
32, 12, 54, 0, 0, 0, 73, 0, 91, 0, 0, 65.27657092, 99,
76, 0, 0, 0, 36.95395031, 0, 88, 44, 0, 0, 86.09277176, 0, 0, 0,
84, 11, 0, 0, 0, 0, 0, 45, 0
), .Dim = c(6L, 6L), .Dimnames = list(
c(
"Indonesia", "Iran", "Iraq", "Ireland",
"Israel", "Italy"
), c(
"Indonesia", "Iran",
"Iraq", "Ireland", "Israel", "Italy"
)
))
modules <- structure(list(
Label = structure(73:78, .Label = c(
"Afghanistan",
"Albania", "Algeria", "Angola", "Antigua and Barbuda", "Argentina",
"Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahrain",
"Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin",
"Bhutan", "Bolivia (Plurinational State of)", "Bosnia and Herzegovina",
"Botswana", "Brazil", "Brunei Darussalam", "Bulgaria", "Burkina Faso",
"Burundi", "C?te d'Ivoire", "Cambodia", "Cameroon", "Canada",
"Central African Republic", "Chile", "China", "China, Hong Kong SAR",
"China, Macao SAR", "China, Taiwan Province of", "Colombia",
"Congo", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czechia",
"Democratic People's Republic of Korea", "Democratic Republic of the Congo",
"Denmark", "Dominican Republic", "Ecuador", "Egypt", "El Salvador",
"Eritrea", "Estonia", "Eswatini", "Ethiopia", "Finland", "France",
"Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Greece", "Grenada",
"Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Honduras",
"Hungary", "India", "Indonesia", "Iran",
"Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan",
"Kazakhstan", "Kenya", "Kuwait", "Kyrgyzstan", "Lao People's Democratic Republic",
"Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Lithuania",
"Luxembourg", "Madagascar", "Malawi", "Malaysia", "Mali", "Malta",
"Mauritania", "Mexico", "Mongolia", "Montenegro", "Morocco",
"Mozambique", "Myanmar", "Namibia", "Nepal", "Netherlands", "New Zealand",
"Nicaragua", "Niger", "Nigeria", "North Macedonia", "Norway",
"Oman", "Pakistan", "Palestine", "Panama", "Papua New Guinea",
"Paraguay", "Peru", "Philippines", "Poland", "Portugal", "Qatar",
"Republic of Korea", "Republic of Moldova", "Romania", "Russian Federation",
"Rwanda", "Saint Kitts and Nevis", "Saint Lucia", "Saint Vincent and the Grenadines",
"Saudi Arabia", "Senegal", "Serbia", "Sierra Leone", "Singapore",
"Slovakia", "Slovenia", "Somalia", "South Africa", "Spain", "Sri Lanka",
"Sudan", "Suriname", "Sweden", "Switzerland", "Syrian Arab Republic",
"Tajikistan", "Thailand", "Timor-Leste", "Trinidad and Tobago",
"Tunisia", "Turkey", "Turkmenistan", "Uganda", "Ukraine", "United Arab Emirates",
"United Kingdom of Great Britain and Northern Ireland", "United Republic of Tanzania",
"United States of America", "Uruguay", "Uzbekistan", "Venezuela (Bolivarian Republic of)",
"Viet Nam", "Yemen", "Zambia", "Zimbabwe"
), class = "factor"),
modularity_class = c(0L, 3L, 2L, 1L, 4L, 4L)
), row.names = c(
"Indonesia",
"Iran", "Iraq", "Ireland", "Israel", "Italy"
), class = "data.frame")