knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 6, fig.height = 5, fig.dpi = 50, dpi = 50 ) library(cograph)
cograph accepts network data in all common formats used in R. Pass any supported object directly to splot() and it will be automatically parsed.
A square numeric matrix where M[i,j] represents the edge weight from node i to node j.
Sources:
cor(), cov() — correlation and covariance matricesqgraph::getWmat() — extract weights from qgraph objectsbootnet::estimateNetwork() — network estimation outputpsychonetrics — structural equation modeling networksmarkovchain — transition probability matricesas.matrix(dist()) — distance/dissimilarity matricesAuto-detection:
rownames()/colnames()Usage:
# Create a weighted adjacency matrix adj_matrix <- matrix( c(0, 0.8, 0.5, 0.2, 0.8, 0, 0.6, 0, 0.5, 0.6, 0, 0.7, 0.2, 0, 0.7, 0), nrow = 4, byrow = TRUE, dimnames = list(c("A", "B", "C", "D"), c("A", "B", "C", "D")) ) # Plot directly from matrix splot(adj_matrix, title = "From Adjacency Matrix") # Create a directed (asymmetric) matrix directed_matrix <- matrix( c(0, 0.9, 0, 0, 0.2, 0, 0.7, 0, 0.5, 0, 0, 0.8, 0, 0.3, 0.4, 0), nrow = 4, byrow = TRUE, dimnames = list(c("A", "B", "C", "D"), c("A", "B", "C", "D")) ) # Directed networks detected automatically splot(directed_matrix, title = "Directed Network (auto-detected)")
A data frame where each row is an edge with source, target, and optional weight columns.
Sources:
igraph::as_data_frame(g, what = "edges")Column detection (case-insensitive):
| Purpose | Recognized names |
|---------|------------------|
| Source | from, source, src, v1, node1, i |
| Target | to, target, tgt, v2, node2, j |
| Weight | weight, w, value, strength |
Falls back to columns 1 and 2 if no match.
Usage:
# Create an edge list data frame edges <- data.frame( from = c("Alice", "Alice", "Bob", "Bob", "Carol", "Dave"), to = c("Bob", "Carol", "Carol", "Dave", "Dave", "Alice"), weight = c(0.9, 0.5, 0.7, 0.3, 0.8, 0.4) ) print(edges) # Plot from edge list splot(edges, title = "From Edge List") # Alternative column names work too edges_alt <- data.frame( source = c("X", "X", "Y", "Z"), target = c("Y", "Z", "Z", "X"), value = c(1, 0.5, 0.8, 0.3) ) splot(edges_alt, title = "Alternative Column Names")
Objects of class igraph from the igraph package.
Sources:
graph_from_data_frame(), graph_from_adjacency_matrix()make_graph("Zachary"), make_ring(), sample_pa(), etc.read_graph() — import from GraphML, GML, Pajek, edge listsintergraph::asIgraph() — convert from network objectsPreserved:
V(g)$name, V(g)$color, etc.E(g)$weight, E(g)$type, etc.is_directed(g)Usage:
# Create igraph objects g_ring <- igraph::make_ring(6) igraph::V(g_ring)$name <- LETTERS[1:6] splot(g_ring, title = "igraph Ring Graph") # Famous graph with vertex names g_zachary <- igraph::make_graph("Zachary") splot(g_zachary, title = "Zachary Karate Club") # Weighted graph g_weighted <- igraph::graph_from_adjacency_matrix( adj_matrix, mode = "undirected", weighted = TRUE ) splot(g_weighted, title = "Weighted igraph")
Objects of class network from the network/statnet ecosystem.
Sources:
network::network() — create from matrix or edge listergm package — exponential random graph modelssna package — social network analysisintergraph::asNetwork() — convert from igraphPreserved:
%v% operator%e% operatoris.directed()Usage:
# Create a network object net_obj <- network::network(adj_matrix, directed = FALSE) splot(net_obj, title = "From statnet network")
Objects created by the qgraph package.
Sources:
qgraph::qgraph(..., DoNotPlot = TRUE)bootnet::estimateNetwork() resultspsychonetrics model outputsPreserved:
q$layoutq$Edgelistq$graphAttributes$NodesUsage:
# Create a qgraph object (without plotting) q <- qgraph::qgraph(adj_matrix, DoNotPlot = TRUE) # Plot with cograph (preserves layout) splot(q, title = "From qgraph Object")
Objects of class tna from the tna package (Transition Network Analysis).
Sources:
tna::tna() — build from sequence datatna::group_tna() — grouped transition analysisExtracted:
$weights$labels$initsUsage:
library(tna) # Build TNA model from included dataset tna_model <- tna(group_regulation) # Plot TNA model directly splot(tna_model, title = "From TNA Model") # With donut nodes showing initial probabilities splot(tna_model, node_shape = "donut", donut_fill = tna_model$inits, title = "TNA with Initial Probabilities")
| Parameter | Effect |
|-----------|--------|
| weight_digits = 2 | Round weights; edges rounding to zero are removed |
| threshold = 0.3 | Remove edges with \|weight\| < threshold |
| minimum = 0.3 | Alias for threshold (qgraph compatibility) |
| maximum = 1.0 | Set reference maximum for edge width scaling |
| edge_scale_mode | "linear", "sqrt", "log", or "rank" |
# Create a network with varying weights weights_matrix <- matrix( c(0, 0.1, 0.5, 0.9, 0.1, 0, 0.2, 0.7, 0.5, 0.2, 0, 0.3, 0.9, 0.7, 0.3, 0), nrow = 4, byrow = TRUE, dimnames = list(LETTERS[1:4], LETTERS[1:4]) ) # Apply threshold to remove weak edges splot(weights_matrix, threshold = 0.4, title = "Threshold = 0.4 (weak edges removed)")
| Feature | Behavior |
|---------|----------|
| Negative weights | Colored differently (green/red by default) |
| Self-loops | Rendered as loops; control angle with loop_rotation |
| Reciprocal edges | Curved apart automatically in directed networks |
| Duplicate edges | Error by default; use edge_duplicates to aggregate |
# Network with negative weights (e.g., correlation matrix) cor_matrix <- matrix( c(1, 0.8, -0.5, 0.3, 0.8, 1, 0.2, -0.7, -0.5, 0.2, 1, 0.4, 0.3, -0.7, 0.4, 1), nrow = 4, byrow = TRUE, dimnames = list(LETTERS[1:4], LETTERS[1:4]) ) diag(cor_matrix) <- 0 splot(cor_matrix, title = "Negative Weights (red = negative)")
cograph provides functions to convert between formats. All conversion functions accept any supported input type.
Convert any format to a cograph_network object.
Accepted inputs:
matrix — Adjacency/weight matrixdata.frame — Edge list with from/to/weight columnsigraph — igraph objectnetwork — statnet network objectqgraph — qgraph objecttna — TNA model objectgroup_tna — Grouped TNA model object# From matrix net <- as_cograph(adj_matrix) print(net) # From edge list net_edges <- as_cograph(edges) print(net_edges) # Override auto-detected directedness net_directed <- as_cograph(adj_matrix, directed = TRUE) cat("Directed:", attr(net_directed, "directed"), "\n")
# From igraph g <- igraph::make_ring(5) igraph::V(g)$name <- LETTERS[1:5] net_from_igraph <- as_cograph(g) print(net_from_igraph)
The function is idempotent — passing a cograph_network returns it unchanged.
Convert any format to an igraph object:
# From matrix g <- to_igraph(adj_matrix) cat("Vertices:", igraph::vcount(g), "\n") cat("Edges:", igraph::ecount(g), "\n") # From cograph_network net <- as_cograph(adj_matrix) g2 <- to_igraph(net) # Check attributes preserved cat("Vertex names:", paste(igraph::V(g2)$name, collapse = ", "), "\n")
Convert any format to an edge list data frame:
# From matrix df <- to_df(adj_matrix) print(df) # From cograph_network net <- as_cograph(adj_matrix) df2 <- to_df(net) print(df2)
Output columns: from, to, weight
Convert any format to an adjacency matrix:
# From cograph_network net <- as_cograph(edges) mat <- to_matrix(net) print(mat)
# From igraph (with weights) g <- igraph::graph_from_adjacency_matrix( matrix(c(0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0), 4, 4, dimnames = list(c("W", "X", "Y", "Z"), c("W", "X", "Y", "Z"))), mode = "undirected", weighted = TRUE ) mat <- to_matrix(g) print(mat)
Returns a square numeric matrix with row/column names preserved.
Convert any format to a statnet network object:
# Convert matrix to statnet network statnet_net <- to_network(adj_matrix) print(statnet_net)
Requires the network package. Preserves edge weights and vertex names.
| Function | Output | Use Case |
|----------|--------|----------|
| as_cograph() / to_cograph() | cograph_network | Import for visualization with splot |
| to_igraph() | igraph | Use igraph's analysis functions |
| to_df() | data.frame | Export to CSV, database, or other tools |
| to_matrix() | matrix | Export to adjacency matrix for other packages |
| to_network() | network | Use with statnet/ergm ecosystem |
| Input | Class | Directed | Weights | Labels |
|-------|-------|----------|---------|--------|
| Matrix | matrix | Symmetry check | Cell values | dimnames |
| Edge list | data.frame | Reciprocal check | weight column | Unique nodes |
| igraph | igraph | is_directed() | weight attr | name attr |
| network | network | is.directed() | weight attr | vertex.names |
| qgraph | qgraph | Edgelist | Edgelist | Node names |
| tna | tna | Always TRUE | $weights | $labels |
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.