选择颜色ggraph R [英] Choose color ggraph R
问题描述
我尝试绘制一个二边网络,其边缘与它们的节点之一具有相同的颜色.
例如,让我们以电影/演员二部图为例,其中有7部电影和15名演员,每个演员都有国籍.
我想让演员和一部比演员国籍相同的颜色的电影之间具有优势.
I try to plot a bipartite network with the edges of the same color than one of their nodes.
For example, let's take an movie/actor bipartite graph as an exemple, with 7 movies and 15 actors, and each actor has a nationality.
I want to have the edge between an actor and a movie of the same color than the nationality of the actor.
NG1 <- 7
NG2 <- 15
Nat <- sample(x = c("French", "English", "Italian", "American", "Chinese"), size = NG2, replace = T)
G <- graph.empty(NG1+NG2)
[这里, head(Nat)
返回意大利语"英语"美国人"法语"法语"法语"
]
创建边列表的代码:
[Here, head(Nat)
returns "Italian" "English" "American" "French" "French" "French"
]
The code to create the edgelist:
E1 <- sample(x=1:NG1, size = 30, replace = T)
E2 <- sample(x=(NG1+1):(NG1+NG2), size = 30, replace = T)
EL <- c(rbind(E1, E2))
G <- add_edges(G, EL, nat = Nat[E2-NG1])
[这里, head(EL)
返回 1 14 3 13 2 15
]
不同的aes参数:
[Here, head(EL)
returns 1 14 3 13 2 15
]
The different aes arguments:
GROUP <- c(rep("Movie", NG1), rep("Act", NG2))
COL <- c(rep("Movie", NG1), Nat)
TXT <- c(as.character(1:NG1), letters[1:NG2])
现在是ggraph指令:
And now the ggraph instructions:
ggraph(G, layout = 'kk') +
geom_node_point(aes(col = COL, shape = GROUP, size = 7)) +
geom_edge_link(aes(col = nat)) +
geom_node_text(aes(label = TXT), size = 4)
从底部可以看到,演员Ita(意大利人)有一个蓝色节点,但与电影7的粉红色边缘相连...如何为节点(这里有6种颜色)指定调色板边缘(节点的前5种颜色)?
As you can see in the bottom, actor a, which is Italien has a blue node, but is connected with a pink edge with movie 7... How can I specify the color palette for nodes (here 6 colors) and edges (the 5 first colors of the nodes)?
我希望我已经说清楚了.
I hope that I have made clear.
推荐答案
两个小时后,我终于找到了解决方案!
After two hours, I finally found a solution !
我使用了此处定义的函数 gg_color_hue
模拟用于节点的6种颜色,然后:
I used the function gg_color_hue
defined here to emulate the 6 colors used for the nodes and then:
+ scale_edge_colour_manual(values = cols[1:5])
这篇关于选择颜色ggraph R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!