Skip to main content
v2026.11,986 entries · CC-BY 4.0

ggraph in R: Network, Tree and Graph Plots with ggplot2

ggraph extends ggplot2 to plot networks, trees and graphs in R: install, build a first directed plot, choose layouts and fix common errors.

Written and maintained by CASRAI Editorial Board

Published · Last updated

On this page

ggraph is an R package that extends ggplot2 to draw networks, graphs and trees layer by layer: you pass it a graph, choose a layout that gives every node an x and y position, then add node and edge geoms the way you would add points and lines to a ggplot. In research it is used to visualise collaboration networks, citation links, hierarchical clusterings and any other relational data, with the current CRAN release being 2.2.2 (published 2025-08-24) and its source being free under the MIT licence.

What ggraph is used for in research

The package site describes ggraph as aimed at supporting relational data structures such as networks, graphs and trees. Where a standard ggplot2 chart maps rows of a data frame to points, ggraph maps a graph to two coordinated pieces: nodes (the entities) and edges (the relationships between them). Typical research inputs are co-authorship or collaboration edge lists, citation or reference links, regulatory or interaction networks, and the hierarchies produced by hierarchical clustering.

Two design choices explain why analysts pick it over drawing a graph with base plotting. First, ggraph has access to all the layout functions available in igraph, plus layouts of its own such as hive plots, treemaps and circle packing, so the same graph object can be viewed as a node-link diagram, a dendrogram or a space-filling treemap by changing one argument. Second, because it follows the ggplot2 grammar, everything you already know about aesthetics, scales, themes and facets carries over; see the guide to ggplot2 plots in R for that foundation, and the igraph guide for building and analysing the underlying graph objects.

How to install or access it

ggraph is distributed on CRAN, the official R package repository. Install the released version from within R:

install.packages('ggraph')

The development version is installed from GitHub with pak, as shown on the package site:

pak::pak('thomasp85/ggraph')

The CRAN page for 2.2.2 lists R 2.10 or later and ggplot2 3.5.0 or later as dependencies, and imports including dplyr, ggforce, igraph (1.0.0 or later), tidygraph, graphlayouts (1.1.0 or later), ggrepel and viridis. Installing from CRAN pulls these in automatically; if installation fails on a clean machine, work through the general steps in how to install libraries in R. Load the package with library(ggraph); because ggraph builds on ggplot2 you also normally call library(ggplot2), and library(tidygraph) when you construct graphs with the tidy interface used below.

Step-by-step: a first directed network plot

The example below is an illustrative composite, not real data: five invented researchers (A to E) in two groups, with directed links such as one person mentoring or citing another. It uses only functions documented on the ggraph and tidygraph reference sites.

1. Build a tbl_graph from a node table and an edge table

tidygraph’s tbl_graph(nodes = NULL, edges = NULL, directed = TRUE, node_key = 'name') constructs the graph; edges refer to nodes through the node key column, name by default. The tidygraph reference describes a tbl_graph as a thin wrapper around an igraph object, so every igraph method still works on it.

library(tidygraph)
library(ggraph)

nodes = data.frame(
  name  = c('A', 'B', 'C', 'D', 'E'),
  group = c('x', 'x', 'y', 'y', 'y')
)
edges = data.frame(
  from = c('A', 'A', 'B', 'C', 'D'),
  to   = c('B', 'C', 'C', 'D', 'E')
)
graph = tbl_graph(nodes = nodes, edges = edges, directed = TRUE)

2. Choose a layout in ggraph()

The layout is set in the first call: ggraph(graph, layout = 'kk') uses Kamada-Kawai. The Layouts vignette defines a layout as the vertical and horizontal placement of nodes, and lists kk (Kamada-Kawai) and fr (Fruchterman-Reingold) among the options; the default is stress, which is deterministic.

3. Add edge and node layers

ggraph(graph, layout = 'kk') +
  geom_edge_link(arrow = arrow(length = unit(4, 'mm')),
                 end_cap = circle(3, 'mm')) +
  geom_node_point(aes(colour = group), size = 5) +
  geom_node_text(aes(label = name), repel = TRUE) +
  theme_void()

Each line maps to something documented. geom_edge_link() draws straight edges; the Edges vignette shows an arrow added with arrow = arrow(length = unit(4, 'mm')) and the edge stopped short of the node centre with end_cap = circle(3, 'mm'), so the arrowhead is not hidden under the node. geom_node_point() takes x and y from the layout by default, so you only map extra aesthetics such as colour. geom_node_text() needs a label aesthetic supplied; setting repel = TRUE hands label placement to ggrepel to reduce overlaps (see the ggrepel guide). theme_void() is the ggplot2 theme with no axes, which suits graphs where x and y carry no meaning.

Layouts and geoms: choosing the right pairing

The most common mistake is treating ggraph as a node-link tool only. The data table below, compiled from the package’s Layouts and Nodes vignettes, pairs each documented layout with the geoms those vignettes use for it. Use it to jump from a research question (show a hierarchy, show cluster membership by area, compare groups on axes) to a working call.

Edge geometry families

The Edges vignette organises edge geoms into families: link, fan, parallel, loop, density, arc, bundle (force, path and minimal variants), elbow, diagonal, bend, hive, span and point/tile, with geom_conn_bundle documented separately. In practice, geom_edge_link() gives straight lines, geom_edge_arc() is the one the Layouts vignette pairs with the linear layout, geom_edge_elbow() with dendrograms, and geom_edge_hive() with hive plots. Useful arguments shown there include start_cap and end_cap (for example circle(3, 'mm')), strength (0 is straight, 1 is the default curvature for the relevant geoms), edge label with angle_calc = 'along' and label_dodge, and a direction gradient with aes(alpha = after_stat(index)).

Node geometry families

The Nodes vignette states that x and y default to the layout and that all node geoms accept a filter aesthetic, for example aes(filter = leaf), to draw only some nodes. Beyond geom_node_point() it documents geom_node_text() and geom_node_label(), geom_node_voronoi() (with max.radius), geom_node_tile(), geom_node_range(), geom_node_circle() and geom_node_arc_bar() for circular partitions.

Worked variation: a dendrogram from hierarchical clustering

A frequent research task is plotting the result of hierarchical clustering. The Layouts vignette builds it directly from base R objects:

dendrogram = hclust(dist(iris[, 1:4]))
ggraph(dendrogram, 'dendrogram', height = height) +
  geom_edge_elbow()

Here iris is R’s built-in example dataset, and the height argument maps the merge heights of the clustering to the tree. For plots of phylogenetic trees specifically, the ggtree guide covers a package built for that purpose.

Output and how to read it

In a node-link layout such as kk, fr or stress, position carries only relative meaning: nodes that are strongly connected tend to be drawn near each other, but absolute x and y values and distances are products of the layout algorithm, not measurements, so do not read axes or interpret a long edge as a stronger relationship. Colour, size and edge aesthetics are where quantitative information should go, and you should state in the figure legend which layout was used. In directed graphs, the arrowhead marks the target; the end cap shows where the edge stops, not where it ends in the data.

In space-filling layouts the logic changes. In the treemap and circle-packing layouts, which take a weight argument, area encodes the weight, and the vignette colours cells by depth in the hierarchy. In a linear layout all nodes lie on one line and arcs show links, and the vignette colours arcs by a variable such as year; with circular = TRUE the layout wraps into a circle and the vignette adds coord_fixed() so the circle is not distorted. In a hive plot, nodes are placed on axes by a grouping variable and sorted along each axis, in the vignette’s example by degree, so position on an axis is meaningful.

Troubleshooting

Node labels do not appear

The Nodes vignette notes that geom_node_text() and geom_node_label() need the label aesthetic supplied; it is not defaulted to the node name. Add aes(label = name).

Circular layouts look squashed

Layouts with circular = TRUE, circle packing and circular partitions are shown in the vignettes with coord_fixed() appended. Add it so the x and y scales share the same unit length.

Edges point at the wrong place or hide under nodes

Use start_cap and end_cap with a shape such as circle(3, 'mm') sized slightly larger than your node, as in the arrow example above.

The graph will not build or an edge refers to an unknown node

With tbl_graph(), edges refer to nodes through node_key (default name). Check that every value in from and to matches a value in that column exactly, including case and whitespace.

Installation or version errors

The CRAN page requires ggplot2 3.5.0 or later. If package loading fails, update ggplot2 and ggraph together; the general procedure is in the installation section above.

Free alternatives

All four packages below are listed on CRAN with the licence shown; descriptions are quoted or paraphrased from each CRAN page.

Package What CRAN says it does CRAN version (date) Licence
igraph Graph construction, analysis and its own plotting; ggraph reuses igraph’s layout functions ggraph requires 1.0.0 or later —
ggnetwork Geometries to plot network objects with ggplot2 0.5.14 (2025-09-10) GPL-3
visNetwork R interface to the vis.js JavaScript library for interactive network visualisation 2.1.4 (2025-09-04) MIT + file LICENSE
networkD3 D3 JavaScript network, tree, dendrogram and Sankey graphs from R 0.4.1 (2025-04-14) GPL (≥ 3)

Choose by output: ggraph and ggnetwork give static, ggplot2-themed figures for manuscripts; visNetwork and networkD3 produce interactive JavaScript output.

Frequently asked questions

What is ggraph?

It is an R package that extends the ggplot2 grammar of graphics to graphs, networks and trees, adding layouts and node and edge geoms. The CRAN description says it extends the ggplot2 API for graph and network visualisation, layer by layer.

Does ggraph need igraph?

Yes in the sense that igraph is listed under Imports on the CRAN page, and ggraph has access to all layout functions available in igraph. tidygraph, which wraps igraph, is also imported.

Which layout is the default?

The Layouts vignette describes stress as the default and as deterministic, meaning repeated runs on the same graph give the same positions.

How do I draw arrows on directed edges?

Pass arrow = arrow(length = unit(4, 'mm')) to an edge geom such as geom_edge_link(), and add end_cap = circle(3, 'mm') so the arrowhead stops at the node boundary.

Can ggraph draw trees and dendrograms?

Yes. The Layouts vignette draws an hclust object with the dendrogram layout and geom_edge_elbow(), and also provides treemap, circle packing and partition layouts.

Sources

  • CRAN, ggraph package page (version 2.2.2, published 2025-08-24): https://cran.r-project.org/web/packages/ggraph/index.html, 2026-11-14
  • ggraph package site: https://ggraph.data-imaginist.com/, 2026-11-14
  • ggraph vignette, Layouts: https://ggraph.data-imaginist.com/articles/Layouts.html, 2026-11-14
  • ggraph vignette, Edges: https://ggraph.data-imaginist.com/articles/Edges.html, 2026-11-14
  • ggraph vignette, Nodes: https://ggraph.data-imaginist.com/articles/Nodes.html, 2026-11-14
  • ggraph reference, geom_node_text: https://ggraph.data-imaginist.com/reference/geom_node_text.html, 2026-11-14
  • tidygraph reference, tbl_graph: https://tidygraph.data-imaginist.com/reference/tbl_graph.html, 2026-11-14
  • CRAN package pages for tidygraph, ggnetwork, visNetwork and networkD3, 2026-11-14

Data: ggraph layouts and the edge and node geoms its vignettes pair with them

Compiled from the Layouts, Edges and Nodes vignettes of the ggraph package site: for each documented layout, the layout-specific arguments and the edge and node geoms used in the vignette example for it. Entries are limited to calls shown in those vignettes.

ggraph layouts and the edge and node geoms its vignettes pair with them
LayoutLayout-specific argumentsEdge geom in vignette exampleNode or other geom in vignette exampleSource vignette
kk (Kamada-Kawai)nonegeom_edge_link()geom_node_point()Layouts
fr (Fruchterman-Reingold)nonegeom_edge_link()geom_node_point()Layouts
dendrogramheight = heightgeom_edge_elbow()none shownLayouts
treemapweight = sizenonegeom_node_tile(aes(fill = depth), size = 0.25)Layouts
circlepackweight = sizenonegeom_node_circle(aes(fill = depth), size = 0.25, n = 50) with coord_fixed()Layouts
linearcircular = TRUE adds coord_fixed()geom_edge_arc(aes(colour = factor(year)))none shownLayouts
hiveaxis = friends, sort.by = degreegeom_edge_hive()geom_axis_hive()Layouts
partition (circular)circular = TRUEnonegeom_node_arc_bar(aes(fill = depth)) with coord_fixed()Nodes

Sources

Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · full access

Ask about ggraph in R: Network, Tree and Graph Plots with ggplot2

Full access: large monthly usage, up to 10 source passages behind each answer, and MCP and API access for Claude Code, Codex and other CLIs. Cancel any time.

An AI assistant for research: funder rules, compliance, methods and publishing. It links the sources behind its answers and labels answers that come from a web search.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays open to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →