Is it possible to scale the size and color of nodes/vertices using ggnetwork based on two variables?

288 Views Asked by At

Working from the 'blood type' example in the documentation (Tyner, Briatte and Hoffman):

> bt <- as.character( unique( blood$edges$from ) )
> bt_df <- data.frame( type=bt, spin=c(1,2,3,4,5,6,7,8),charm=c(8,7,6,5,4,3,2,1))
Browse[1]> bt_df
  type spin charm
1  AB-    1     8
2  AB+    2     7
3   A-    3     6
4   A+    4     5
5   B-    5     4
6   B+    6     3
7   O-    7     2
8   O+    8     1

Is it possible to plot the network graph varying the color of vertices across some palette based on the value of "spin" and varying vertex size based on the value of "charm"?

1

There are 1 best solutions below

0
Mark Bower On

Found the answer in the documentation along with some trial-and-error. The key step is "geom_nodes(size=bt_df$spin, color=bt_df$charm)".

ggplot( ggnetwork(grph, layout="circle", arrow.gap=0.05),aes(x,y,xend=xend,yend=yend)) +
          geom_edges(color="grey50", arrow=arrow(length=unit(10,"pt"), type="closed")) +
          scale_color_brewer(palette = "Set2") +
          geom_nodes(size=bt_df$spin, color=bt_df$charm) +
          geom_nodetext(aes(label=vertex.names), color="grey80") + 
          theme_blank() +
          coord_fixed( 0.5 )