Nothing
knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 6, fig.height = 6, fig.dpi = 50, dpi = 50 )
cograph is a modern R package for network visualization. It provides a clean, pipe-friendly API for creating publication-quality network plots.
library(cograph)
cograph accepts several input formats:
# Create a simple adjacency matrix adj <- matrix(c( 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0 ), nrow = 5, byrow = TRUE) # Add labels rownames(adj) <- colnames(adj) <- c("A", "B", "C", "D", "E") # Create and render cograph(adj)
edges <- data.frame( from = c("Alice", "Alice", "Bob", "Carol"), to = c("Bob", "Carol", "Carol", "Dave"), weight = c(1, 2, 1, 3) ) cograph(edges)
cograph provides several layout algorithms:
cograph(adj, layout = "circle")
cograph(adj, layout = "spring", seed = 42)
groups <- c(1, 1, 2, 2, 2) cograph(adj) |> sn_layout("groups", groups = groups)
cograph(adj) |> sn_nodes( size = 0.08, shape = "circle", fill = "steelblue", border_color = "navy", border_width = 2 )
cograph(adj) |> sn_nodes( size = c(0.05, 0.06, 0.08, 0.06, 0.05), fill = c("red", "orange", "yellow", "green", "blue") )
Available shapes: circle, square, triangle, diamond, pentagon, hexagon, ellipse, star, heart, pie, cross.
cograph(adj) |> sn_nodes( shape = c("circle", "square", "triangle", "diamond", "star") )
cograph(adj) |> sn_edges( width = 2, color = "gray40", alpha = 0.7 )
For weighted networks, edge width and color can be mapped to weights:
# Create weighted adjacency matrix weighted <- matrix(c( 0, 0.8, -0.5, 0, 0, 0.8, 0, 0.3, -0.7, 0, -0.5, 0.3, 0, 0.6, -0.4, 0, -0.7, 0.6, 0, 0.9, 0, 0, -0.4, 0.9, 0 ), nrow = 5, byrow = TRUE) cograph(weighted) |> sn_edges( width = "weight", color = "weight", positive_color = "darkgreen", negative_color = "darkred" )
# Create directed network dir_adj <- matrix(c( 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 ), nrow = 5, byrow = TRUE) cograph(dir_adj, directed = TRUE) |> sn_edges( curvature = 0.1, arrow_size = 0.015 )
cograph includes several built-in themes:
cograph(adj) |> sn_theme("classic")
cograph(adj) |> sn_theme("dark")
cograph(adj) |> sn_theme("minimal")
cograph(adj) |> sn_theme("colorblind")
Convert your network to a ggplot2 object for additional customization:
library(ggplot2) p <- cograph(adj) |> sn_nodes(fill = "coral") |> sn_ggplot() p + labs( title = "My Network", subtitle = "Created with cograph" ) + theme(plot.title = element_text(hjust = 0.5))
# Social network example social <- matrix(c( 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0 ), nrow = 6, byrow = TRUE) rownames(social) <- colnames(social) <- c("Alice", "Bob", "Carol", "Dave", "Eve", "Frank") groups <- c("Team A", "Team A", "Team A", "Team B", "Team B", "Team B") social |> cograph() |> sn_layout("groups", groups = groups) |> sn_nodes( size = 0.06, fill = ifelse(groups == "Team A", "#E69F00", "#56B4E9"), border_width = 2 ) |> sn_edges(width = 1.5, alpha = 0.6) |> sn_theme("minimal") |> sn_render(title = "Social Network")
net <- cograph(adj) |> sn_nodes(fill = "steelblue") |> sn_theme("minimal") # Save as PDF sn_save(net, "network.pdf", width = 8, height = 8) # Save as PNG sn_save(net, "network.png", width = 8, height = 8, dpi = 300) # Save as SVG sn_save(net, "network.svg", width = 8, height = 8)
list_layouts()list_shapes()list_themes()Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.