Gongsoonyee
Gongsoonyee

Reputation: 138

keeping the shape of the graph, but change the color

I am using as_tbl_graph and ggraph to show the network relation data. I wish to keep the shape and location of each node, while changing the color with several conditions. Problem here is that every time I run the code, the full graph image changes at all. Is there a way to keep the shape of the graph, but only change the color? There are so many items so I can't designate each node's location.

Upvotes: 0

Views: 46

Answers (1)

caldwellst
caldwellst

Reputation: 5956

You can use the layout argument in ggraph to fix the graph structure, then change anything else you'd like. You can find more in the Details section of the ?ggraph help page, in the vigentte("Layouts", package = "ggraph") and in articles like this one.

library(ggraph)
library(igraph) # for sample data

graph <- graph_from_data_frame(highschool)

ggraph(graph, layout = "kk") +
  geom_edge_link(aes(color = factor(year))) +
  geom_node_point()

enter image description here

Let's say you wanted something else, so we change the layout.

p_stress <- ggraph(graph, layout = "stress") +
  geom_edge_link(aes(color = factor(year)))

p_stress + geom_node_point()

enter image description here Okay, clearly it's different from our first graph using layout = "kk". Now we can adjust the color and size of points (or whatever else we want) without changing the graph structure.

p_stress + geom_node_point(color = "blue", size = 3)

enter image description here

Upvotes: 1

Related Questions