Movatterモバイル変換


[0]ホーム

URL:


Nodes

Thomas Lin Pedersen

2025-08-24

Nodes in a network are the entities that are connected. Sometimesthese are also referred to as vertices, butggraph hasopted for this nomenclature and uses it consistently. While the nodes ina graph are the abstract concepts of entities, and the layout is theirphysical placement, the node geoms are the visual manifestation of theentities. Conceptually one can simply think of it in terms of a scatterplot — the layout provides the x and y coordinates, and these can beused to draw nodes in different ways in the plotting window. Actually,due to the design ofggraph the standardscatterplot-like geoms fromggplot2 can be useddirectly for plotting nodes:

library(ggraph)library(tidygraph)set_graph_style(plot_margin =margin(1,1,1,1))gr<-as_tbl_graph(highschool)ggraph(gr,layout ='kk')+geom_point(aes(x = x,y = y))

The reason this works is that, as discussed in the Layout vignette,layouts return adata.frame of node positions and metadataand this is used as the default plot data:

head(create_layout(gr,layout ='kk'))
## # A tibble: 6 × 5##        x     y .ggraph.orig_index circular .ggraph.index##    <dbl> <dbl>              <int> <lgl>            <int>## 1  2.43  1.30                   1 FALSE                1## 2  2.81  1.79                   2 FALSE                2## 3  3.40  1.24                   3 FALSE                3## 4 -2.46  0.932                  4 FALSE                4## 5 -1.66  1.98                   5 FALSE                5## 6 -0.234 3.34                   6 FALSE                6

geom_node_*()

While usage of the defaultggplot2 is absolutelyallowed,ggraph comes with its own set of node geoms. Manyof these are direct translations ofggplot2 own geoms likegeom_point() so one could wonder why bother to usethem.

The first reason is to provide clear code. It is not apparentanywhere that the standard geoms are addressing the nodes and usinggeom_node_*() makes it clear that this layer will drawnodes.

The second reason is that it will save typing. Sinceggraph is in control of the shape of the input data throughthe layout calculations, it knows thatx andyposition is encoded in anx andy column. Thismeans thatgeom_node_* can default the x and y aestheticsso there’s no need to type them:

ggraph(gr,layout ='kk')+geom_node_point()

sometimes there is a need for addressing the x and y aesthetics,which is still possible, for instance if a partition layout should beinverted:

gr<-tbl_graph(flare$vertices, flare$edges)ggraph(gr,layout ='partition')+geom_node_tile(aes(y =-y,fill = depth))

of course this could also be accomplished by reversing the y-axisusingscale_y_reverse() so this is just to illustrate thatthe defaults are easily overwritten if needed.

The third reason is for the added functionality. Allggraph geoms get afilter aesthetic thatallows you to quickly filter the input data. The use of this can beillustrated when plotting a tree:

ggraph(gr,layout ='dendrogram',circular =TRUE)+geom_edge_diagonal()+geom_node_point(aes(filter = leaf))+coord_fixed()

In the above plot only the terminal nodes are drawn by filtering onthe logical leaf column provided by the dendrogram layout.

The different node geoms

The usual suspects are of course provided in the form ofgeom_node_point() (showcased above),geom_node_text(), andgeom_node_label(). Thesework as expected, taking in the usual aesthetics (plusfilter).Only x and y are defaulted so everything else must be providede.g. label which does not default to thename column likeis done inigraph. One feature setsgeom_node_text() andgeom_node_label() apartfrom theirggplot2 counterparts: both have arepel argument that, when set toTRUE, willuse the repel functionality provided by theggrepel package toavoid overlapping text. There is alsogeom_node_voronoi()that plots nodes as cells from a voronoi tesselation. This is useful fore.g. showing dominance of certain node types in an area as overlappingis avoided:

graph<-create_notable('meredith')|>mutate(group =sample(c('A','B'),n(),TRUE))ggraph(graph,'stress')+geom_node_voronoi(aes(fill = group),max.radius =1)+geom_node_point()+geom_edge_link()+coord_fixed()

Apart from these geoms there’s a set of geoms mainly useful forspatial node layouts such as treemaps, partition, circle packing, andfabric.geom_node_tile() andgeom_node_range()are theggraph counterpart toggplot2sgeom_tile() andgeom_linerange() whilegeom_node_circle() andgeom_node_arc_bar()maps toggforcesgeom_circle() andgeom_arc_bar(). Collective for these is that the spatialdimensions of the geoms (e.g. radius, width, and height) areprecalculated by their intended layouts and defaulted by the geoms:

ggraph(gr,layout ='treemap',weight = size)+geom_node_tile(aes(fill = depth))

All spatial node geoms will be center-based, meaning that the x and yvalue of the layout will refer to the center of the layout and note.g. the bottom-left corner. This makes it easier to add labels tospatial layouts as well as using spatial layouts in a non-spatialway:

l<-ggraph(gr,layout ='partition',circular =TRUE)l+geom_node_arc_bar(aes(fill = depth))+coord_fixed()

l+geom_edge_diagonal()+geom_node_point(aes(colour = depth))+coord_fixed()

More node geoms are sure to appear inggraph with timebut they will generally be quite easily comprehensible due to theirstrong resemblance to the standardggplot2 geoms. After allit is just points on a plane…

Want more?

Check out the other vignettes for more information on how to specifylayouts and drawedges


[8]ページ先頭

©2009-2025 Movatter.jp