R GGally 按顶点成员为边缘着色
R GGally color edges by vertex membership
我正在尝试创建一个列表,根据图形边缘所连接的顶点的分组为图形的边缘着色。如果一条边连接同一组的两个成员,则该边具有它们的颜色。如果边缘位于不同组的成员之间,则边缘应为灰色。这是我的代码:
mnet = rgraph(10, mode = "graph", tprob = 0.5)
rownames(mnet)<-letters[1:10]
colnames(mnet)<-letters[1:10]
net = network(mnet, directed = FALSE, ignore.eval = FALSE, names.eval = "strength")
phono<-data.frame(letters=letters[1:10],phono=c("vowel", "consonant","consonant","consonant","vowel","consonant","consonant","consonant","vowel", "consonant"))
table(phono)
net %v% "phono" <- as.character(phono$phono)
nnet<-matrix(0, 10, 10)
for (i in 1:10){
for (j in 1:10){
nnet[i,j] = ifelse(phono$phono[phono$letters %in% rownames(mnet)[i]]==phono$phono[phono$letters %in% colnames(mnet)[j]],
as.character(phono$phono[phono$letters %in% rownames(mnet)[i]]),
"grey50")
}
}
ggnet2(net, color = "phono", palette = "Set2", label = "phono", edge.color = unlist(nnet[upper.tri(nnet)])[unlist(mnet[upper.tri(mnet)])>0])
我对 R 代码中间的双循环不满意。有没有更好的方法来做我想做的事情?这是搭载我之前的问题更多试图理解为什么 "outer" 没有工作。
另外,代码不是一般的,但是让我们一次做一道题。虽然
length(unlist(nnet[upper.tri(nnet)])[unlist(mnet[upper.tri(mnet)])>0])==length(net %e% "strength")
谢谢。
这是您要找的吗?
代码:
# build graph
net <- sna::rgraph(10, mode = "graph", tprob = 0.5)
net <- network::network(net, directed = FALSE)
# vowels and consonants
phono <- c("v", "c", "c", "c", "v", "c", "c", "c", "v", "c")
names(phono) <- letters[1:10]
# extract edgelist, identify same-letter-type ties
edges <- as.edgelist(net)
k <- ifelse(phono[ edges[, 1] ] == phono[ edges[, 2] ], phono[ edges[, 1] ], NA)
# color ties using `Set2` colors
k[ is.na(k) ] <- "x"
k <- c("c" = "#66C2A5", "v" = "#FC8D62", "x" = "grey50")[ k ]
set.edge.attribute(net, "tie.type", k)
GGally::ggnet2(
net,
color = phono[ network.vertex.names(net) ],
palette = "Set2",
label = phono[ network.vertex.names(net) ],
edge.color = "tie.type",
edge.size = 1
)
我正在尝试创建一个列表,根据图形边缘所连接的顶点的分组为图形的边缘着色。如果一条边连接同一组的两个成员,则该边具有它们的颜色。如果边缘位于不同组的成员之间,则边缘应为灰色。这是我的代码:
mnet = rgraph(10, mode = "graph", tprob = 0.5)
rownames(mnet)<-letters[1:10]
colnames(mnet)<-letters[1:10]
net = network(mnet, directed = FALSE, ignore.eval = FALSE, names.eval = "strength")
phono<-data.frame(letters=letters[1:10],phono=c("vowel", "consonant","consonant","consonant","vowel","consonant","consonant","consonant","vowel", "consonant"))
table(phono)
net %v% "phono" <- as.character(phono$phono)
nnet<-matrix(0, 10, 10)
for (i in 1:10){
for (j in 1:10){
nnet[i,j] = ifelse(phono$phono[phono$letters %in% rownames(mnet)[i]]==phono$phono[phono$letters %in% colnames(mnet)[j]],
as.character(phono$phono[phono$letters %in% rownames(mnet)[i]]),
"grey50")
}
}
ggnet2(net, color = "phono", palette = "Set2", label = "phono", edge.color = unlist(nnet[upper.tri(nnet)])[unlist(mnet[upper.tri(mnet)])>0])
我对 R 代码中间的双循环不满意。有没有更好的方法来做我想做的事情?这是搭载我之前的问题更多试图理解为什么 "outer" 没有工作。
另外,代码不是一般的,但是让我们一次做一道题。虽然
length(unlist(nnet[upper.tri(nnet)])[unlist(mnet[upper.tri(mnet)])>0])==length(net %e% "strength")
谢谢。
这是您要找的吗?
代码:
# build graph
net <- sna::rgraph(10, mode = "graph", tprob = 0.5)
net <- network::network(net, directed = FALSE)
# vowels and consonants
phono <- c("v", "c", "c", "c", "v", "c", "c", "c", "v", "c")
names(phono) <- letters[1:10]
# extract edgelist, identify same-letter-type ties
edges <- as.edgelist(net)
k <- ifelse(phono[ edges[, 1] ] == phono[ edges[, 2] ], phono[ edges[, 1] ], NA)
# color ties using `Set2` colors
k[ is.na(k) ] <- "x"
k <- c("c" = "#66C2A5", "v" = "#FC8D62", "x" = "grey50")[ k ]
set.edge.attribute(net, "tie.type", k)
GGally::ggnet2(
net,
color = phono[ network.vertex.names(net) ],
palette = "Set2",
label = phono[ network.vertex.names(net) ],
edge.color = "tie.type",
edge.size = 1
)