Plot of the degree distribution of a graph in R

Asked

Viewed 124 times

1

I have a graph and I want to plot the degree distribution. Example:

> library(igraph)
> g <- make_ring(10)
> degree(g)
 [1] 2 2 2 2 2 2 2 2 2 2
> g2 <- sample_gnp(1000, 10/1000)
> degree_distribution(g2)
 [1] 0.000 0.000 0.001 0.008 0.017 0.045 0.061 0.096 0.103 0.133 0.127 0.102 0.103 0.069 0.046 0.039 0.026 0.012 0.007
[20] 0.000 0.003 0.002

I want to plot the distribution of degrees with log scale, more or less like in the image below:

inserir a descrição da imagem aqui

1 answer

2


You can request the log scale directly in the Plot command:

g2   <- sample_gnp(1000, 10/1000)
dg2  <- degree_distribution(g2)
x  <- 1:max(degree(g2)) - 1
zeros <- (dg2 == 0) # para remover os zeros (pois log(0) é infinito)
plot(x[!zeros], dg2[!zeros], log = "xy", 
     xlab = "Log Degree", 
     ylab = "Log Frequency")

inserir a descrição da imagem aqui

The graph of your example will not match your drawing because you simulated the Erdos-Renyi model. Your drawing follows a power law.

For a simulation more like what you want:

g2   <- sample_fitness_pl(10000, 30000, 2.2, 2.3)
dg2  <- degree_distribution(g2)
x  <- 1:max(degree(g2)) - 1
zeros <- dg2 == 0
plot(x[!zeros], dg2[!zeros], log = "xy", 
     xlab = "Log Degree", 
     ylab = "Log Frequency")

inserir a descrição da imagem aqui

  • Great Carlos. But I have two questions. First:How to fix the Y axis between 0 and 1? That is, I would like q to stay: 0, 0.2, 0.4, 0.6, 0.8, 1 (since the probability varies between 0 and 1. Second: How do I have the line that accompanies the point (line power fit)?

  • You can adjust the limits of the Y-axis with the option ylim = c(1e-05, 1), you cannot put zero literally because you are taking the log out, and log(0) is infinite. As for the regression line, after adjusting the regression you want you can put with abline(modelo) as suggested here: https://answall.com/a/6269/5010 but remember that this may not be the best way to estimate power law.

  • What is the best way to estimate power law?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.