Instantly share code, notes, and snippets.

# ruliana/igraph-degree-distribution.R

Last active Jul 7, 2022
Plotting degree distribution with igraph and ggplot2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 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()

### ParamjitGill commented Mar 3, 2020

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

Paramjit Gill

Very useful!