Plotting the degree distribution
- 1.Import some classes from JFreeChart and Breeze:
- import org.jfree.chart.axis.ValueAxis
- import breeze.linalg._
- import breeze.plot._
- 2.Define degreeHistogram function.
- def degreeHistogram(net: Graph[Int, Int]): Array[(Int, Int)] =
- net.degrees.map(t => (t._2,t._1)).
- groupByKey.map(t => (t._1,t._2.size)).
- sortBy(_._1).collect()
- 3.Normalize the node degrees by the total number of nodes, so that the degree probabilities add up to one:
- val nn = egoNetwork.numVertices
- val egoDegreeDistribution = degreeHistogram(egoNetwork).map({case (d,n) => (d,n.toDouble/nn)})
- 4.Display the degree distribution
- val f = Figure()
- val p1 = f.subplot(2,1,0)
- val x = new DenseVector(egoDegreeDistribution map (_._1.toDouble))
- val y = new DenseVector(egoDegreeDistribution map (_._2))
- p1.xlabel = "Degrees"
- p1.ylabel = "Distribution"
- p1 += plot(x, y)
- p1.title = "Degree distribution of social ego network"
- val p2 = f.subplot(2,1,1)
- val egoDegrees = egoNetwork.degrees.map(_._2).collect()
- p1.xlabel = "Degrees"
- p1.ylabel = "Histogram of node degrees"
- p2 += hist(egoDegrees, 10)
复制代码