[英]Setting edge attributes conditionally in list-column graphs using R igraph and dplyr (purrr)
我有一個數據框,其中包含一系列列列格式的igraph
對象。 我想有條件地設置邊緣顏色屬性。
我已經包含了dput
包含三個圖形的實際數據幀(非常大,數千個圖形)的示例版本的dput
輸出。 它仍然很長,因此我將其放在本文的底部,我將解釋到目前為止我嘗試過的一些想法。
首先嘗試使用purrr
包對mutate
和map
進行多次使用。
sampleColored <- sampleGraphs %>% mutate(map(graph, function(x)
E(x)[weights == 0]$color = "blue")) %>% mutate(map(graph, function(x)
E(x)[weights < 0]$color = "red")) %>% mutate(map(graph, function(x)
E(x)[weights > 0]$color = "green"))
沒有錯誤信息,但是命令
shortPlots <- sampleColored %>%
mutate(plots = map(graph, function(x) plot(x, layout=layout.circle,
vertex.size=20,
edge.curved=TRUE)))
產生了漂亮的圖形,所有邊緣均為灰色。
同樣,在第二次嘗試中,我創建了edgeColor
函數並使用了單個map
調用。
edgecolor <- function(x) {
E(x)[weights == 0]$color <- "blue"
E(x)[weights < 0]$color <- "red"
E(x)[weights > 0]$color <- "green"
return(E(x))
}
sampleColored <- sampleGraphs %>% mutate(map(graph, function(x) edgecolor(x)))
沒有錯誤和灰色邊緣。 刪除mutate
命令會產生錯誤消息:
Error in as.numeric(n): cannot coerce type 'closure' to vector of type 'double'
我相信這是可能的,而且我只是不了解要使用正確的語法。 任何建議將不勝感激。 感謝您的光臨。
這是sampleGraph
dput
:
sampleGraphs <- structure(list(ID = 997:1000, graph = list(structure(list(5,
TRUE, c(0, 1, 2, 0, 3, 4, 1, 2, 4, 3, 0, 4, 2, 3, 0, 1, 3,
1, 4, 2), c(1, 0, 0, 4, 1, 1, 4, 3, 0, 2, 3, 2, 1, 4, 2,
3, 0, 2, 3, 4), c(0, 14, 10, 3, 1, 17, 15, 6, 2, 12, 7, 19,
16, 4, 9, 13, 8, 5, 11, 18), c(1, 2, 16, 8, 0, 12, 4, 5,
14, 17, 9, 11, 10, 15, 7, 18, 3, 6, 19, 13), c(0, 4, 8, 12,
16, 20), c(0, 4, 8, 12, 16, 20), list(c(1, 0, 1), structure(list(), .Names = character(0)),
structure(list(name = c("3", "0", "2", "4", "1")), .Names = "name"),
structure(list(weights = c(3L, -4L, 4L, -3L, 43L, 8L,
4L, 14L, 1L, 55L, 2L, 22L, 26L, 64L, 9L, 2L, 13L, -12L,
25L, 16L)), .Names = "weights")), <environment>), class = "igraph"),
structure(list(5, TRUE, c(0, 1, 2, 2, 1, 3, 1, 3, 4, 3, 3,
0, 4, 0, 4, 4, 2, 1, 2, 0), c(3, 3, 4, 0, 2, 1, 4, 2, 0,
4, 0, 2, 1, 4, 2, 3, 3, 0, 1, 1), c(19, 11, 0, 13, 17, 4,
1, 6, 3, 18, 16, 2, 10, 5, 7, 9, 8, 12, 14, 15), c(17, 3,
10, 8, 19, 18, 5, 12, 11, 4, 7, 14, 0, 1, 16, 15, 13, 6,
2, 9), c(0, 4, 8, 12, 16, 20), c(0, 4, 8, 12, 16, 20), list(
c(1, 0, 1), structure(list(), .Names = character(0)),
structure(list(name = c("2", "0", "1", "3", "4")), .Names = "name"),
structure(list(weights = c(4L, -4L, 25L, 22L, 4L, 3L,
2L, -3L, 55L, 2L, 9L, 16L, 43L, 14L, 64L, 13L, 1L, -12L,
8L, 26L)), .Names = "weights")), <environment>), class = "igraph"),
structure(list(5, TRUE, c(0, 1, 2, 3, 4, 0, 1, 2, 1, 3, 1,
3, 2, 4, 2, 4, 0, 0, 3, 4), c(1, 4, 3, 4, 0, 4, 2, 0, 0,
2, 3, 1, 4, 1, 1, 2, 3, 2, 0, 3), c(0, 17, 16, 5, 8, 6, 10,
1, 7, 14, 2, 12, 18, 11, 9, 3, 4, 13, 15, 19), c(8, 7, 18,
4, 0, 14, 11, 13, 17, 6, 9, 15, 16, 10, 2, 19, 5, 1, 12,
3), c(0, 4, 8, 12, 16, 20), c(0, 4, 8, 12, 16, 20), list(
c(1, 0, 1), structure(list(), .Names = character(0)),
structure(list(name = c("4", "0", "3", "2", "1")), .Names = "name"),
structure(list(weights = c(43L, 4L, 9L, 16L, 25L, 64L,
-4L, 2L, 2L, 4L, -11L, 26L, -3L, 8L, 3L, 1L, 55L, 13L,
14L, 22L)), .Names = "weights")), <environment>), class = "igraph"),
structure(list(5, TRUE, c(0, 1, 2, 3, 4, 1, 3, 2, 4, 0, 1,
3, 2, 4, 0, 0, 2, 4, 1, 3), c(4, 4, 4, 1, 2, 0, 2, 3, 0,
3, 2, 0, 1, 1, 2, 1, 0, 3, 3, 4), c(15, 14, 9, 0, 5, 10,
18, 1, 16, 12, 7, 2, 11, 3, 6, 19, 8, 13, 4, 17), c(5, 16,
11, 8, 15, 12, 3, 13, 14, 10, 6, 4, 9, 18, 7, 17, 0, 1, 2,
19), c(0, 4, 8, 12, 16, 20), c(0, 4, 8, 12, 16, 20), list(
c(1, 0, 1), structure(list(), .Names = character(0)),
structure(list(name = c("1", "4", "0", "2", "3")), .Names = "name"),
structure(list(weights = c(1L, 13L, -4L, 14L, 3L, 64L,
26L, -11L, -3L, 22L, 43L, 16L, 2L, 2L, 8L, 25L, 4L, 8L,
55L, 4L)), .Names = "weights")), <environment>), class = "igraph"))), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -4L), .Names = c("ID",
"graph"))
使用set_edge_attr
而不是igraph
的慣用E()
邊緣函數會set_edge_attr
幫助。 我不得不將sampleGraph
列表修改為一個簡單的圖形列表,將其升級到igraph
的較新版本,但這igraph
:
graphs <- sampleGraphs$graph
graphs <- lapply(graphs, function(x) upgrade_graph(x)) #making a simple list of graphs
edgecolor <- function(x) {
E(x)[weights == 0]$color <- "blue"
E(x)[weights < 0]$color <- "red"
E(x)[weights > 0]$color <- "green"
return(E(x)$color)
} #The function now returns a list of colors conditional on statements
#Pass the function to the "values" argument of "set_edge_attr"
graphs_colored <- graphs %>% map(., function(x) set_edge_attr(x, "color", value = edgecolor(x)))
par(mfrow = c(2,2), mar = c(0,0,0,0))
shortPlots <- graphs_colored %>%
map(., function(x) plot(x,
layout=layout.circle,
vertex.size=20,
edge.curved=TRUE,
edge.arrow.size = 0.5))
得到它了! 感謝@paqmo的建議。 我需要使用mutate重新定義graph
list-column變量。
edgecolor <- function(x) {
E(x)[weights == 0]$color <- "#FF000000"
E(x)[weights < 0]$color <- "red"
E(x)[weights > 0]$color <- "green"
return(E(x)$color)
}
sampleColored <- sampleGraphs %>% mutate(graph = map(graph, function(x)
set_edge_attr(x, "color", value = edgecolor(x))))
par(mfrow = c(2,2), mar = c(0,0,0,0))
samplePlots <- sampleColored %>%
mutate(plots = map(graph, function(x) plot(x, layout=layout.circle,
vertex.size=20,
edge.curved=TRUE)))
生成與@paqmo相同的圖像。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.