Skip to content

Instantly share code, notes, and snippets.

@fusaroli
Created June 30, 2020 07:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fusaroli/4b8b81be22de52b7bea310a2b77c8a6e to your computer and use it in GitHub Desktop.
Save fusaroli/4b8b81be22de52b7bea310a2b77c8a6e to your computer and use it in GitHub Desktop.
# Assuming you have a matrix (CorMat_S) with a structure like a correlation matrix
# Load the libraries (NB probably some superfluous ones, I haven't double-checked)
pacman::p_load(
# General utility functions
tidyverse,
# Plotting
ggplot2,
scales,
ggstance,
# Networks
corrr,
igraph,
ggraph
)
# Define graph options
set_graph_style(plot_margin = margin(1,1,1,1))
cut.off <- 0.5 # here it removes all correlations between -0.5 and 0.5
# Build the graph
net <- graph_from_data_frame(CorMat_S,directed = FALSE)
# Tidy up the graph
net <- simplify(net, remove.multiple = F, remove.loops = T)
# Remove the edges below cut point
net.SOC <- delete_edges(net, E(net)[weight < cut.off])
# Define the layout as circular
l <- layout_in_circle(net.SOC)
# Create the plot
soc_P <- plot(net.SOC,
layout = l,
edge.width=E(net.SOC)$weight*6, # beef up the edges to see differences in weight
vertex.shape = "none", # Remove nodes (cleaner plot)
edge.curved = TRUE, # curved edges because they are pretty
directed = F, # no arrows
vertex.label.degree=100) # orientation of the labels
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment