summaryrefslogtreecommitdiffstats
path: root/R Scripts/plot-power-law.R
diff options
context:
space:
mode:
Diffstat (limited to 'R Scripts/plot-power-law.R')
-rw-r--r--R Scripts/plot-power-law.R26
1 files changed, 26 insertions, 0 deletions
diff --git a/R Scripts/plot-power-law.R b/R Scripts/plot-power-law.R
new file mode 100644
index 0000000..8cca228
--- /dev/null
+++ b/R Scripts/plot-power-law.R
@@ -0,0 +1,26 @@
+plot(1:max(degree(lcc)),degree.distribution(lcc)[-1],
+ log='xy',col='#377EB8',pch=20,
+ xlab='Degree', ylab='Number of Vertices', main='')
+
+
+# plot and fit the power law distribution
+# calculate degree
+d = degree(graph, mode = "all")
+dd = degree.distribution(graph, mode = "all", cumulative = FALSE)
+degree = 1:max(d)
+probability = dd[-1]
+# delete blank values
+nonzero.position = which(probability != 0)
+probability = probability[nonzero.position]
+degree = degree[nonzero.position]
+reg = lm(log(probability) ~ log(degree))
+cozf = coef(reg)
+power.law.fit = function(x) exp(cozf[[1]] + cozf[[2]] * log(x))
+alpha = -cozf[[2]]
+print(paste("Alpha =", round(alpha, 3)))
+# plot
+print(d)
+curve(power.law.fit, col = "red", add = T, n = length(d))
+
+
+# fit_power_law(lcc)