Chapter 7 nodes

7.1 Node attributes

As with the edge attributes, you can set and map different node attributes. Let’s look at those lemurs again. Data from this wonderful website.

lemur_graphml <- read.graph("https://raw.githubusercontent.com/bansallab/asnr/master/Networks/Mammalia/lemur_sexual_weighted/lemur_pereira_sexual_network_1_attribute.graphml", format = "graphml")

lemur <- as_tbl_graph(lemur_graphml)

7.1.1 node colour

Setting the colour of all nodes:

ggraph(lemur, layout = "kk") +
  geom_edge_link(colour = "grey") +
  geom_node_point(colour = "purple", size = 4) +
  theme_graph()

Mapping a variable to node colour:

ggraph(lemur, layout = "kk") +
  geom_edge_link(colour = "grey") +
  geom_node_point(aes(colour = SEX), size = 4) +
  theme_graph()

Let’s choose our own colours:

ggraph(lemur, layout = "kk") +
  geom_edge_link(colour = "grey") +
  geom_node_point(aes(colour = SEX), size = 4) +
  theme_graph() +
  scale_colour_manual(values = c("#CC79A7", "#56B4E9"))

7.1.1.1 node shape

ggraph(lemur, layout = "kk") +
  geom_edge_link(colour = "grey") +
  geom_node_point(aes(shape = SEX), size = 4) +
  theme_graph()

You can always choose to combine multiple “mappings”:

ggraph(lemur, layout = "kk") +
  geom_edge_link(colour = "grey") +
  geom_node_point(aes(shape = SEX, colour = SEX), size = 4) +
  theme_graph()

7.1.1.2 node size

ggraph(lemur, layout = "kk") +
  geom_edge_link(colour = "grey") +
  geom_node_point(aes(size = SEX, colour = SEX)) +
  theme_graph()
## Warning: Using size for a discrete variable is not advised.

7.1.2 stroke

This refers to the outside border of the node.

ggraph(lemur, layout = "kk") +
  geom_edge_link(colour = "grey") +
  geom_node_point(aes(stroke = if_else(SEX=="MALE", 1, 2)), 
                  fill = "grey", colour = "black", size = 4, shape = 21) +
  theme_graph()

7.2 Node labels

ggraph(lemur, layout = "kk") +
  geom_edge_link(colour = "grey") +
  geom_node_point(aes(colour = SEX), size = 10) +
  geom_node_text(aes(label = id), colour = "white") +
  theme_graph() +
  scale_colour_manual(values = c("#CC79A7", "#56B4E9"))

Different variant:

ggraph(lemur, layout = "kk") +
  geom_edge_link(colour = "grey") +
  geom_node_label(aes(fill = SEX, label = id), colour = "white") +
  theme_graph() +
  scale_colour_manual(values = c("#CC79A7", "#56B4E9"))

7.3 DIY

Try to make something fancy for the Zebras! Data from this wonderful website.

zebra_graphml <- read.graph("https://raw.githubusercontent.com/bansallab/asnr/master/Networks/Mammalia/zebra_groupmembership_weighted/zebra_sundaresan_interaction_attribute.graphml", format = "graphml")

zebra <- as_tbl_graph(zebra_graphml)