Skip to content

Instantly share code, notes, and snippets.

@antonkratz
Forked from ruliana/igraph-degree-distribution.R
Created July 21, 2018 01:55
Show Gist options
  • Save antonkratz/e1676d8bf243a591dd2eda1c014eba98 to your computer and use it in GitHub Desktop.
Save antonkratz/e1676d8bf243a591dd2eda1c014eba98 to your computer and use it in GitHub Desktop.
Plotting degree distribution with igraph and ggplot2
# Anton Kratz
# date: Sat Jul 21 17:03:22 PDT 2018
suppressPackageStartupMessages(library("igraph"))
suppressPackageStartupMessages(library("ggplot2"))
# Load the PG Yeast Net file - two large components and many small ones
rf <- read.table("~/git/assets/INT.PG.YeastNet.v3.2463gene.54496link.txt", sep="\t")
akg <- graph_from_data_frame(rf, directed = FALSE, vertices = NULL)
# List of degrees
d <- degree(akg)
# Let's count the frequencies of each degree
d.histogram <- as.data.frame(table(d))
# Need to convert the first column to numbers, otherwise
# the log-log thing will not work (that's fair...)
d.histogram[,1] <- as.numeric(d.histogram[,1])
# Now, plot it!
ggplot(d.histogram, aes(x = d, 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)")
ggsave("degree.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment