Plot degree distribution in a network

generate a random graph

library(igraph)
set.seed(123)
g.ba<-barabasi.game(10000,directed=F)
plot(g.ba,layout=layout.circle,vertex.label=NA)

d<-degree(g.ba)
hist(d,col='lightblue',xlab='Degree',ylab='Frequency',main='')

summary(d)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##       1       1       1       2       2      67

log-log plot

dt<-table(d)
df=data.frame(degree=names(dt),count=as.numeric(dt))
df$freq=df$count/sum(df$count)
library(ggplot2)
df$degree=as.numeric(as.character(df$degree))
ggplot(df,aes(x=df$degree,y=df$freq))+geom_point()+theme_bw()+xlab('Degree')+ylab('Freq')

ggplot(df,aes(x=df$degree,y=df$freq))+geom_point()+theme_bw()+xlab('Degree')+ylab('Freq')+scale_x_log10()+scale_y_log10()

logarithmic binning plot

bin.step=2
max(df$count)
## [1] 6056
log(max(df$count),base=bin.step);
## [1] 12.56415
up=floor(log(max(df$count),base=bin.step))

bin.size=2^(0:up)
bin.left.bound=cumsum(2^(0:up))
bucket = cut(d,breaks = bin.left.bound,right = FALSE,labels = c(paste0('< ',scales::comma(c(2^(1:up))))))
x<-table(bucket)
df<-data.frame(bin.left.bound=bin.left.bound[1:length(x)],count=as.numeric(x),label=names(x))
df$freq=df$count/sum(df$count)
ggplot(df[df$freq!=0,],aes(x=bin.left.bound,y=freq))+geom_point()+theme_bw()+xlab('Degree')+ylab('Freq')+scale_x_log10()+scale_y_log10()+ggtitle('log2 binning')

cumulative density plot and complementary cumulative distribution function

dt<-table(d)
df=data.frame(degree=names(dt),count=as.numeric(dt))
df$freq=df$count/sum(df$count)
df$cumfreq = cumsum(df$freq)
df$degree=as.numeric(as.character(df$degree))
ggplot(df,aes(x=df$degree,y=df$cumfreq))+geom_point()+theme_bw()+xlab('Degree')+ylab('Freq')+scale_x_log10()+scale_y_log10()+ggtitle("CDF plot on log-log scale")

df$ccdf=1-df$cumfreq
ggplot(df,aes(x=df$degree,y=df$ccdf))+geom_point()+theme_bw()+xlab('Degree')+ylab('Freq')+scale_x_log10()+scale_y_log10()+ggtitle("CCDF plot on log-log scale")

installed.packages()[names(sessionInfo()$otherPkgs), "Version"]
##    igraph   forcats   stringr     dplyr     purrr     readr     tidyr    tibble 
##   "1.2.6"   "0.5.1"   "1.4.0"  "1.0.10"   "1.0.0"   "2.0.0"   "1.1.3"   "3.1.8" 
##   ggplot2 tidyverse     knitr 
##   "3.3.5"   "1.3.1"    "1.33"