简体   繁体   English

R 网络分析:边和顶点的颜色

[英]R Network Analysis: Color of edges and vertices

I have created a graph with the igraph-Package in R.我用 R 中的 igraph-Package 创建了一个图表。 创建图表

Now i want to make the graph more colorful.现在我想让图表更加丰富多彩。 I want to color my network based on the type of "party" in my dataset.我想根据我的数据集中的“派对”类型为我的网络着色。 My dataset looks like this:我的数据集如下所示:

    Screen.name     mentions           party
  1 @A_Gloeckner    @MartinSchulz      SPD
  2 @A_Gloeckner    @MartinSchulz      SPD
  3 @A_Gloeckner    @MartinSchulz      SPD
  4 @A_Gloeckner    @ManuelaSchwesig   SPD
  5 @A_Gloeckner    @sigmargabriel     SPD
  6 @A_Gloeckner    @nahlesMeine       SPD
  ... ...           ...                ...
 33 @A_Schillhaneck @BA_Mitte_Berlin   GRNE
 34 @A_Schillhaneck @nytimes           GRNE
 35 @A_Schillhaneck @nutellaberliner   GRNE

This is the code how i have created my graph:这是我创建图表的代码:

gj <- graph.data.frame(joined_df, directed = TRUE)
plot(gj,
 vertex.label = NA,
 vertex.size = 2,
 edge.arrow.size = 0.1,
 vertex.label.cex = 0.8,
 layout = layout.fruchterman.reingold)

The graph:图表:

> gj
IGRAPH c5ba2ee DN-- 1279 2147 -- 
+ attr: name (v/c), color (v/c), party (e/c)
+ edges from c5ba2ee (vertex names):
[1] @A_Gloeckner->@MartinSchulz    @A_Gloeckner->@MartinSchulz    
@A_Gloeckner->@MartinSchulz   
[4] @A_Gloeckner->@ManuelaSchwesig @A_Gloeckner->@sigmargabriel   
@A_Gloeckner->@nahlesMeine    
[7] @A_Gloeckner->@Willy           @a_grotheer ->@NSC_CPMR        @a_grotheer 
 ->@SouthendRNLI   
[10] @a_grotheer ->@weserkurier     @a_grotheer ->@werderbremen    
@a_grotheer ->@ribasdiego10        
+ ... omitted several edges

Now i want the edges and vertices to be an own color for every different "party".现在我希望边缘和顶点成为每个不同“派对”的自己的颜色。 In this case i just got two different parties ("SPD", "GRNE").在这种情况下,我只有两个不同的方(“SPD”,“GRNE”)。 I want every node and vertice with the "party"-Value "SPD" to be red, and every value with "GRNE" to be green.我希望每个带有“party”-Value“SPD”的节点和顶点都是红色的,每个带有“GRNE”的值都是绿色的。

Does anybody know how to do this?有人知道怎么做这个吗?

I know that i can change the colors with for example vertex.color = "red" or edge.color = "red" , but i don't know how to set the color on dependencies.我知道我可以使用例如vertex.color = "red"edge.color = "red"更改 colors ,但我不知道如何设置依赖项的颜色。

Thanks in advance for your help!在此先感谢您的帮助!

You can use set_edge_attr like below您可以使用set_edge_attr如下

graph.data.frame(joined_df, directed = TRUE) %>%
  set_edge_attr(name = "color", value = c("red", "green")[match(joined_df$party, c("SPD", "GRNE"))]) %>%
  plot(
    vertex.label = NA,
    vertex.size = 2,
    edge.arrow.size = 0.1,
    vertex.label.cex = 0.8,
    layout = layout.fruchterman.reingold
  )

在此处输入图像描述


If you have more colors and want to color edges automatically, you can try如果您有更多 colors 并想自动为边缘着色,您可以尝试

graph.data.frame(joined_df, directed = TRUE) %>%
  set_edge_attr(name = "color", value = factor(joined_df$party)) %>%
  plot(
    vertex.label = NA,
    vertex.size = 2,
    edge.arrow.size = 0.1,
    vertex.label.cex = 0.8,
    layout = layout.fruchterman.reingold
  )

where the factor should be used over party for the values of color.其中该factor应用于颜色值而不是party

在此处输入图像描述

dummy data虚拟数据

> dput(joined_df)
structure(list(Screen.name = c("@A_Gloeckner", "@A_Gloeckner",
"@A_Gloeckner", "@A_Gloeckner", "@A_Gloeckner", "@A_Gloeckner",
"@A_Schillhaneck", "@A_Schillhaneck", "@A_Schillhaneck"), mentions = c("@MartinSchulz",
"@MartinSchulz", "@MartinSchulz", "@ManuelaSchwesig", "@sigmargabriel",
"@nahlesMeine", "@BA_Mitte_Berlin", "@nytimes", "@nutellaberliner"
), party = c("SPD", "SPD", "SPD", "SPD", "SPD", "SPD", "GR<dc>NE",
"GR<dc>NE", "GR<dc>NE")), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "33", "34", "35"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM