Reputation: 555
I have created a graph with the igraph-Package in R.
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"). I want every node and vertice with the "party"-Value "SPD" to be red, and every value with "GRNE" to be green.
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.
Thanks in advance for your help!
Upvotes: 1
Views: 774
Reputation: 101014
You can use set_edge_attr
like below
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
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.
> 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"))
Upvotes: 2