Skip to content

Instantly share code, notes, and snippets.

@ruliana
Last active July 7, 2022 15:32
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ruliana/8bac0e980b1e5d5bbd289d7ff34f38ab to your computer and use it in GitHub Desktop.
Save ruliana/8bac0e980b1e5d5bbd289d7ff34f38ab to your computer and use it in GitHub Desktop.
Plotting degree distribution with igraph and ggplot2
library("igraph")
library("poweRlaw")
library("ggplot2")
# Just loading my data
edge_list <- read.csv("edge-list-ocupacoes.csv")
G <- graph.data.frame(edge_list)
# List of degrees
G.degrees <- degree(G)
# Let's count the frequencies of each degree
G.degree.histogram <- as.data.frame(table(G.degrees))
# Need to convert the first column to numbers, otherwise
# the log-log thing will not work (that's fair...)
G.degree.histogram[,1] <- as.numeric(G.degree.histogram[,1])
# Now, plot it!
ggplot(G.degree.histogram, aes(x = G.degrees, y = Freq)) +
geom_point() +
scale_x_continuous("Degree\n(nodes with this amount of connections)",
breaks = c(1, 3, 10, 30, 100, 300),
trans = "log10") +
scale_y_continuous("Frequency\n(how many of them)",
breaks = c(1, 3, 10, 30, 100, 300, 1000),
trans = "log10") +
ggtitle("Degree Distribution (log-log)") +
theme_bw()
@eisaac26
Copy link

Very useful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment