R 网络分析:边和顶点的颜色
R Network Analysis: Color of edges and vertices
我在 R 中用 igraph-Package 创建了一个图表。
现在我想让图表更加丰富多彩。我想根据我的数据集中的“派对”类型为我的网络着色。我的数据集如下所示:
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
这是我创建图形的代码:
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)
图表:
> 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
现在我希望边缘和顶点对于每个不同的“派对”来说都是自己的颜色。在这种情况下,我只有两个不同的政党(“SPD”、“GRNE”)。
我希望每个带有“party”-Value“SPD”的节点和顶点都是红色的,每个带有“GRNE”的值都是绿色的。
有人知道怎么做吗?
我知道我可以更改颜色,例如 vertex.color = "red"
或 edge.color = "red"
,但我不知道如何设置依赖项的颜色。
在此先感谢您的帮助!
您可以像下面这样使用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
)
如果你的颜色比较多,想自动给边上色,可以试试
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
)
其中 factor
应该用于 party
颜色值。
虚拟数据
> 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"))
我在 R 中用 igraph-Package 创建了一个图表。
现在我想让图表更加丰富多彩。我想根据我的数据集中的“派对”类型为我的网络着色。我的数据集如下所示:
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
这是我创建图形的代码:
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)
图表:
> 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
现在我希望边缘和顶点对于每个不同的“派对”来说都是自己的颜色。在这种情况下,我只有两个不同的政党(“SPD”、“GRNE”)。 我希望每个带有“party”-Value“SPD”的节点和顶点都是红色的,每个带有“GRNE”的值都是绿色的。
有人知道怎么做吗?
我知道我可以更改颜色,例如 vertex.color = "red"
或 edge.color = "red"
,但我不知道如何设置依赖项的颜色。
在此先感谢您的帮助!
您可以像下面这样使用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
)
如果你的颜色比较多,想自动给边上色,可以试试
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
)
其中 factor
应该用于 party
颜色值。
虚拟数据
> 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"))