Show Sidebar Hide Sidebar

Network Graph in R

How to make network graphs in R with Plotly.

New to Plotly?

Plotly's R library is free and open source!
Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode.
We also have a quick-reference cheatsheet (new!) to help you get started!

Version Check

Version 4 of Plotly's R package is now available!
Check out this post for more information on breaking changes and new features available in this version.

## [1] '4.5.2'

Read Graph File

We are using the well-known social network of Zachary's karate club. GML format file can be collected from here.


data(karate, package="igraphdata")
G <- upgrade_graph(karate)
L <-

Create Vertices and Edges

vs <- V(G)
es <-

Nv <- length(vs)
Ne <- length(es[1]$V1)

Create Nodes

Xn <- L[,1]
Yn <- L[,2]

network <- plot_ly(x = ~Xn, y = ~Yn, mode = "markers", text = vs$label, hoverinfo = "text")

Creates Edges

edge_shapes <- list()
for(i in 1:Ne) {
  v0 <- es[i,]$V1
  v1 <- es[i,]$V2

  edge_shape = list(
    type = "line",
    line = list(color = "#030303", width = 0.3),
    x0 = Xn[v0],
    y0 = Yn[v0],
    x1 = Xn[v1],
    y1 = Yn[v1]

  edge_shapes[[i]] <- edge_shape

Create Network

axis <- list(title = "", showgrid = FALSE, showticklabels = FALSE, zeroline = FALSE)

p <- layout(
  title = 'Karate Network',
  shapes = edge_shapes,
  xaxis = axis,
  yaxis = axis

# Create a shareable link to your chart
# Set up API credentials:
chart_link = api_create(p, filename="karate-network-r")


See for more information and chart attribute options!

Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.