This article refers to Li Ming's "R Language and website analysis" a book
The following basic concepts of social network analysis are implemented using R language
# (1) point set (VERTEXS) and point attribute data
# Use V (g) to return a bit of the set V in the Relational network G, and return the number of points n directly through the length function. The code is as follows:
V (G.undir)
# # Vertex Sequence:
# # [1] 1 2 3 4 5 6 7
Length (V (G.undir))
# # [1] 7
# The point Name Attribute Data V (g.undir) $label is recorded in G.undir, which can also be shown directly here. The code is as follows:
V (G.undir) $label
# # [1] "a" "E" "B" "C" "D" "F" "G"
Note that attribute data can be added arbitrarily in point data V (g.undir). For example, add a group label menbership to a point, and assign one color (color) to the points within each group, respectively. The code is as follows:
Member<-spinglass.community (G.undir)
V (g.undir) $membership <-member$membership; V (G.undir) $membership
# # [1] 3 1 1 1 2 2 2
Mem.col<-rainbow (Length (Unique (V (g.undir) $membership)), alpha=0.3)
V (G.undir) $color <-mem.col[v (G.undir) $membership]; V (G.undir) $color
# # [1] "#0000FF4C" "#FF00004C" "#FF00004C" "#FF00004C" "#00FF004C" "#00FF004C"
# # [7] "#00FF004C"
Plot (G.undir,edge.width=e (G.undir) $weight, Vertex.color=v (g.undir) $color)
# among them, Spinglass.community () is a function that discovers the community, and the membership column data in the returned object is the classification group number for each sample point.
# (2) Click Filter and delete
# Point Data V (G.undir) reads like a vector, whereas a point's attribute Data V (g.undir) $label itself a vector. For example, you can use V (g.undir) [1:2] and V (g.undir) $label [1:2] to read the contents of the first two sample points.
V (G.undir) [1:2]
# # Vertex Sequence:
# # [1] 1 2
V (G.undir) $label [1:2]
# # [1] "a" "E"
# You can also use the which function to filter specific point data and point property data. For example, read the first set of point Data V (g.undir) and the Point Name V (g.undir) $label. The code is as follows:
V (g.undir) [Which (V (g.undir) $membership ==1)]
# # Vertex Sequence:
# # [1] 2 3 4
V (g.undir) $label [which (V (g.undir) $membership ==1)]
# # [1] "E" "B" "C"
# The above code can be simply written as follows:
V (G.undir) [Membership==1]
# # Vertex Sequence:
# # [1] 2 3 4
V (G.undir) [Membership==1] $label
# # [1] "E" "B" "C"
# using the minus sign (g-v (g0[i)), you can remove part of the point V (g) [i] from the relational network G, which is the inverse operation of the add.vertices function. For example, to delete an outlier in a relational network G.undir (not associated with any other store), use the following code:
G.undir<-g.undir-v (G.undir) [degree (g.undir) ==0]
# If you want to delete all the point data within the first group, use the following code:
G.undir<-g.undir-v (G.undir) [Membership==1]
# (3) adjacent points collection
# in the Igraphe package, you can use the neighbors function to find the ordinal vectors of the neighboring points of a point. For example, find the point adjacent to Point D and the name of the point in the G.undir diagram. The code is as follows:
G.undir<-init.graph (data)
Plot (G.undir,edge.width=e (G.undir) $weight, main= "No graph G.undir",
Edge.label=e (G.undir) $weight)
V (G.undir) [Neighbors (G.undir,v=which (V (g.undir) $label = = "D"))]
# # Vertex Sequence:
# # [1] 4 6 7
V (G.undir) $label [Neighbors (G.undir,v=which (V (g.undir) $label = = "D"))]
# # [1] "C" "F" "G"
# for the G.dir graph, the mode parameter can be set to make the neighbors calculation.
G.dir<-init.graph (data,dir=t)
Plot (G.dir,edge.width=e (G.undir) $weight, main= "Map G.undir",
Edge.label=e (G.undir) $weight)
# Find the adjacent out point for Point D (Point D is the initiator of the line: Edges (d,f) and Edges (d,g)), the code is as follows:
V (G.dir) $label [Neighbors (G.dir,v=which (V (g.dir) $label = = "D"), mode= "out")]
# # [1] "F" "G"
# Find the adjacent in point D (Point D is the receiving end of the line: Edges (c,d) and Edges (f,d)), the code is as follows:
V (G.dir) $label [Neighbors (G.dir,v=which (V (g.dir) $label = = "D"), mode= "in")]
# # [1] "C" "F"
# Find the sum of the neighboring points of Point D and the adjacent in points (Point D is the initiator or receiver of the line: Edges (d,f), Edges (d,g), edges (c,d) and Edges (f,d)), the code is as follows:
V (G.dir) $label [Neighbors (G.dir,v=which (V (g.dir) $label = = "D"), mode= "Total")]
# # [1] "C" "F" "F" "G"
# (4) The degree of the point.
# 1) The point degree of the graph without direction. In an no-map, the degree of a point is equal to the number of points adjacent to that point. For example, the degree of midpoint D in the G.undir graph is:
Length (Neighbors (G.undir,v=which (V (g.undir) $label = = "D")))
# # [1] 3
# in the Igraph package you can also use the degree function directly to calculate.
# # # Set Parameter v # # # #
Degree (G.undir,v=which (V (g.undir) $label = = "D"))
# # [1] 3
# # # Vector Way # # #
degree (g.undir) [Which (V (g.undir) $label = = "D")]
# # [1] 3
# where Dgree (G.undir) returns the degree of all points in the relational network G.undir in the form of a vector.
Degree (g.undir)
# # [1] 2 3 2 4 3 1 1
# 2) The point degree of the direction graph. For a g.dir graph, you can also set the mode parameter to specify how the degree function is calculated. The following code calculates the degrees, degrees, and degrees of a point D, respectively.
# # # Set Parameters v Way # # #
Degree (G.dir,v=which (V (g.dir) $label = = "D"), mode= "out")
# # [1] 2
Degree (G.dir,v=which (V (g.dir) $label = = "D"), mode= "in")
# # [1] 2
Degree (G.dir,v=which (V (g.dir) $label = = "D"), mode= "Total")
# # [1] 4
# # # # Vector Way # # #
Degree (g.dir,mode= "out") [Which (V (g.dir) $label = = "D")]
# # [1] 2
Degree (g.dir,mode= "in") [Which (V (g.dir) $label = = "D")]
# # [1] 2
Degree (g.dir,mode= "Total") [Which (V (g.dir) $label = = "D")]
# # [1] 4
# (5) attribute data for line sets and lines
# use E (g) to return the set E (edges) of all lines in the relational network G, and return the number of lines of m directly through the length function.
# no-map G.undir
E (g.undir); Length (E (g.undir))
# # Edge Sequence:
##
# # [1] 2--1
# # [2] 4--1
# # [3] 3--2
# # [4] 4--2
# # [5] 4--3
# # [6] 5--4
# # [7] 6--5
# # [8] 7--5
# # [1] 8
# map G.dir
E (g.dir); Length (E (g.dir))
# # Edge Sequence:
##
# # [1] 1--2
# # [2] 1--4
# # [3] 2--4
# # [4] 3--2
# # [5] 3--4
# # [6] 4--5
# # [7] 5--6
# # [8] 5--7
# # [9] 6--5
# # [1] 9
# (6) Filtering and deletion of lines
# lines filter and delete operations are similar to point data. For example, to read a line with a centerline weight greater than 1 in the G.undir graph, the code is as follows:
E (G.undir) [weight>1]
# # Edge Sequence:
##
# # [7] 6--5
# # [8] 7--5
# You can also use the minus sign (G-E (g) [I] form) to remove the function of the partial line E (g) [i] from the relational network G, which is the inverse of the add.edges function. The following code removes lines with a line weight greater than 1 from the G.undir graph.
G.UNDIR<-G.UNDIR-E (G.undir) [weight>1]
Plot (G.undir)
# (7) Shortest path and distance between two points
# Use the Get.shortest.paths function in the Igraph package to read the shortest path between two points.
G.undir<-init.graph (data)
Get.shortest.paths (G.undir,from=v (g.undir) [label== "a"])
# # $vpath
# # $vpath [[1]]
# # numeric (0)
##
# # $vpath [[2]]
# # [1] 1 2
##
# # $vpath [[3]]
# # [1] 1 4 3
##
# # $vpath [[4]]
# # [1] 1 4
##
# # $vpath [[5]]
# # [1] 1 4 5
##
# # $vpath [[6]]
# # [1] 1 4 5 6
##
# # $vpath [[7]]
# # [1] 1 4 5 7
##
##
# # $epath
# # NULL
##
# # $predecessors
# # NULL
##
# # $inbound _edges
# # NULL
Get.shortest.paths (G.undir,from=v (g.undir) [label== "a"],to=v (g.undir) [label== "B"])
# # $vpath
# # $vpath [[1]]
# # [1] 1 4 3
##
##
# # $epath
# # NULL
##
# # $predecessors
# # NULL
##
# # $inbound _edges
# # NULL
# for a graph, you can set the calculation to a value of "in", "Out", and "all".
# In addition, use the Shortest.paths function in the Igraph package to read the distance between two points (the length of the shortest path).
Shortest.paths (G.undir)
# # [, 1] [, 2] [, 3] [, 4] [, 5] [, 6] [, 7]
# # [1,] 0 1 2 1 2 4 4
# # [2,] 1 0 1 1 2 4 4
# # [3,] 2 1 0 1 2 4 4
# # [4,] 1 1 1 0 1 3 3
# # [5,] 2 2 2 1 0 2 2
# # [6,] 4 4 4 3 2 0 4
# # [7,] 4 4 4 3 2 4 0
Shortest.paths (G.undir,v=v (g.undir) [label== "A"]) # parameter V is the starting point of the path
# # [, 1] [, 2] [, 3] [, 4] [, 5] [, 6] [, 7]
# # [1,] 0 1 2 1 2 4 4
Shortest.paths (G.undir,v=v (g.undir) [label== "a"],to=v (g.undir) [label== "B"])
# # [, 1]
# # [1,] 2
# If you want to draw the shortest path between the G.undir midpoint C point G to the red line and bold, and the related points that the path passes are set to green. The code is as follows:
Pa<-get.shortest.paths (G.undir,from=which (V (g.undir) $label = = "C"),
To=which (V (g.undir) $label = = "G")) [[1]][[1]]
E (g.undir) $color <-"BLACK";
E (G.UNDIR,PATH=PA) $color <-"Red"
E (G.UNDIR,PATH=PA) $width <-3
E (G.UNDIR) [PA] $color <-"Green"
Plot (G.undir)
# code E (G.UNDIR,PATH=PA) is used to intercept related lines passing through the PA points (by setting the path parameter of the E function), the result is as follows:
Pa
# # [1] 4 5 7
E (G.UNDIR,PATH=PA)
# # Edge Sequence:
##
# # [6] 5--4
# # [8] 7--5
# (8) screening of relational networks
# Use the Induced.subgraph function to filter out a relational network by point. For example, to filter out a graph g.undir the midpoint f,d,g,c of the non-direction graph. You can use the following code:
G.unidr<-init.graph (data)
Tmp.v<-c (which (V (g.undir) $label ==c ("C")), which (V (g.undir) $label ==c ("D")),
Which (V (g.undir) $label ==c ("G")), which (V (g.undir) $label ==c ("F")))
G.undir.1<-induced.subgraph (G.undir,v (G.undir) [TMP.V])
# can also be used G.undir.1<-induced.subgraph (G.undir,v (G.undir) [TMP.V])
# Use the Subgraph.edges function to get a relational network through continuous filtering. For example, from the G.undir graph, filter out connections between connection points C and G:
Pa<-get.shortest.paths (G.undir,from=which (V (g.undir) $label = = "C"),
To=which (V (g.undir) $label = = "G")) [[1]][[1]]
G.undir.2<-subgraph.edges (G.undir,e (G.UNDIR,PATH=PA))
# or filter the lines that qualify significant to 1 (the connection between points d,f,g):
G.undir.3<-subgraph.edges (G.undir,which (E (g.undir) $weight >1))
# Finally, use the following statement to show the filtered network diagram.
Par (Mfrow=c (2,2))
Plot (G.undir,edge.width=e (G.undir) $weight, main= "G.undir's relational Network Diagram")
Plot (G.undir.1,edge.width=e (G.undir.1) $weight, f,d,g,c in main= "G.undir")
Plot (G.undir.2,edge.width=e (g.undir.2) $weight, main= "g.undir lines between C and G")
Plot (G.undir.3,edge.width=e (G.UNDIR.3) $weight, main= line with filter weight greater than 1 in G.undir)
Par (mfrow=c ())
R language: Social network relationship analysis-Advanced