Using color in ggnet

506 views Asked by At

I have a network that I made like this:

structure(list(myfirstcol = c(1L, 2L, 3L, 1L, 2L, 3L, 2L, 3L, 
1L), mysecondcol = c(1L, 2L, 3L, 2L, 3L, 2L, 1L, 1L, 3L), value = c(394L, 
445L, 83L, 2L, 12L, 19L, 33L, 84L, 12L)), class = "data.frame", row.names = c(NA, 
-9L))

I used this code to convert it into a network object

library(GGally)
netval1 <-
  network(netmat1_matrix, matrix.type = "edgelist",
          ignore.eval = F, names.eval = "value")

I believe my code should create a network from my edgelist and create a tie attribute called value

I'd like to visualize this matrix and set the node colors to dark blue when the number is big and light blue with the number is small

I tried this code

ggnet(netval1, color = "value")

I just get three nodes and three ties and everything is black. Even if I replace color with "blue" or "green", it doesn't effect the chart.

1

There are 1 answers

0
Werner Hertzog On

When you created your network object, you declared that names.eval = "value", which sets "values" as the edge attribute names. You're asking us how to change the color of the nodes. To do that, you'll have to create a new vertex with node attributes.

Here is an example. Let's just say that some nodes are dark blue and some are blue:

netval1 %v% "nodecolor" = rep(c("darkblue", "blue"))

ggnet2(netval1, color = "nodecolor")

This resulted in the network graph below: enter image description here

Now to answer your second comment:

I'd like to visualize this matrix and set the node colors to dark blue when the number is big and light blue with the number is small

There are no node attributes in your matrix. There's a difference between node and edge attributes. Your matrix is an edge list, so there's only information about edges (i.e, the ties) there. There's no information that we could use to color the nodes, so unfortunately it's impossible do exactly what you want.

Before you change node colors, you'll have to create node attributes. This information can either come from a separate vector or can be added as an attribute to your network object like I did above. You can find examples of how to do this here.