Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Plotting degree distribution with igraph and ggplot2
# Just loading my data
edge_list <- read.csv("edge-list-ocupacoes.csv")
G <-
# List of degrees
G.degrees <- degree(G)
# Let's count the frequencies of each degree <-
# Need to convert the first column to numbers, otherwise
# the log-log thing will not work (that's fair...)[,1] <- as.numeric([,1])
# Now, plot it!
ggplot(, 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)") +
Copy link

Hi Ruliana,
I found that
does not convert the factor degree values to correct numerical values. You may have to use
as.numeric( paste([,1]))

Paramjit Gill

Copy link

Very useful!

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