I've adapted the following code, taken from here to display a weighted and undirected network graph:
# Create data
set.seed(1)
data <- matrix(sample(0:3, 25, replace=TRUE), nrow=5)
data[lower.tri(data)] <- NA
rownames(data) <- LETTERS[1:5]
colnames(data) <- LETTERS[1:5]
# Transform it in a graph format
network <- graph_from_adjacency_matrix(data, weighted = TRUE)
# Remove edges with NA weights
network <- delete_edges(network, E(network)[is.na(E(network)$weight)])
# Make the graph
ggraph(network) +
geom_edge_link(aes(edge_width=E(network)$weight), edge_colour="black", edge_alpha=0.3) +
geom_node_point(color="#69b3a2", size=5) +
geom_node_text(aes(label=name), repel = TRUE, size=8, color="#69b3a2") +
theme_void() +
theme(
legend.position="none",
plot.margin=unit(rep(1,4), "cm")
)
However, in my case, lower values indicate stronger connections, and so I'm trying to work out how to create a graph where smaller values have thicker edges, i.e. to inverse the values of network$weight
. In the example graph here, D>C would have the thickest edge, and A>C the thinnest.
If all your weights are positive, you could flip the values with a helper function like
flip <- function(x) max(x)-x+1
flip(c(10, 9, 1))
# [1] 1 2 10
and then use that when mapping the edge weights
geom_edge_link(aes(edge_width=flip(weight)), edge_colour="black", edge_alpha=0.3)