Scales
grapher has a small but sufficient system of scales similar to that of ggplot2. In grapher scales apply to:
- nodes color
- nodes size
- links source and target color
Then again we will use the data generated by the make_data
function.
g <- make_data() # mock data
head(g)
#> $nodes
#> # A tibble: 100 x 4
#> id label size color
#> <chr> <chr> <dbl> <chr>
#> 1 1 A79 13 #12a2a8
#> 2 2 S19 23 #ffbf50
#> 3 3 A79 39 #8a60b0
#> 4 4 I61 28 #6f63bb
#> 5 5 D30 17 #2ca030
#> 6 6 D82 13 #ff7f0e
#> 7 7 W75 15 #c7519c
#> 8 8 K63 22 #2ca030
#> 9 9 W75 40 #bcbd22
#> 10 10 I61 15 #c7519c
#> # … with 90 more rows
#>
#> $links
#> # A tibble: 100 x 4
#> source target weight hidden
#> <chr> <chr> <dbl> <lgl>
#> 1 1 85 6.68 FALSE
#> 2 2 69 6.48 FALSE
#> 3 3 22 4.03 FALSE
#> 4 4 93 2.55 FALSE
#> 5 5 89 6.42 FALSE
#> 6 6 51 4.51 FALSE
#> 7 7 54 6.68 FALSE
#> 8 8 93 3.60 FALSE
#> 9 9 54 4.98 FALSE
#> 10 10 42 8.22 FALSE
#> # … with 90 more rows
Node size
If we want to scale the node size given the data we generated we could use scale_node_size
which takes two arguments, 1) the bare variable
to scale, 2) and the range
of node size to which variable
should be scaled.
graph(g) %>%
scale_node_size(size, range = c(20, 150))
Node color
We can also scale the color of the nodes to given variable
, we've actually already used this in the guide on clustering. We'll repeat that example below.
Note the palette
argument which takes any vector of colors of your choosing, though it must be said that it is strict with hexadecimals; use the full hex e.g.: #ffffff
for white rather than the shorter #fff
or it will produce an error.
graph(g) %>%
graph_cluster() %>%
scale_node_color(cluster, palette = c("red", "white", "blue"))
But you can of course scale on any variable
included in the nodes meta data.
graph(g) %>%
scale_node_color(size)
You can also color nodes according to their x
, y
, and z
coordinates with scale_node_color_coords
. This function rescales the coordinates and uses them as red, green, blue in the RGB model. Meaning nodes at the extreme x
coordinate will be red, etc. You can redefine those color spaces with the red
, green
, and blue
arguments.
Note that because it relies on coordinates these must have been computed already. It will thus not work with a live layout.
graph(g) %>%
graph_static_layout() %>%
scale_node_color_coords()
Links color
In grapher, the color of a link is actually split in two:
- The color from the source node to the middle of the link.
- The color from the middle of the link to the target node.
This is something that is again genius in ngraph (the underlying JavaScript library). It's incredibly simple yet extremely powerful. Thanks to this split, one can easily define links color according to meta data in the nodes (source and target).
There are four scaling functions that will let you adjust those colors.
scale_link_source_color
scale_link_target_color
scale_link_color
scale_link_color_coords
In the list above, the first two are straight forward, they allow you to scale the links color according to meta data belonging to the links. The other two are slightly more interesting, scale_link_color
will let you color the links according to meta data held in the nodes at either end of each respective link.
We could, for instance, color the links according to the cluster to which nodes at either end of each link belong. This results, in a much better looking graph.
graph(g) %>%
graph_cluster() %>%
scale_link_color(cluster)
Finally you can color links according to the position of nodes at either end. Just as for scale_node_color_coords
, the coordinates of the nodes must have been computed in order to for this method to apply.
graph(g) %>%
graph_offline_layout() %>%
scale_link_color_coords()