Network Analysis among the US Senators

Ruby Ye
5 min readApr 11, 2019

--

The network of the following/followed relationships among the 45 US Senators is shown below.

Twitter network among the US Senators
Code for the number of edges

After cleaning the redundant rows in the dataset, the number of edges of the network is 946. It means that there are 946 following or followed connections among the 45 senators. On average, every senator has 21 connections with other 44 senators.

In-degree analysis

The highest in-degree node is Senator David Perdue @sendavidperdue‏. It means the U.S. Senator of Georgia is followed by most of the other senators on Twitter (43 out of 44).

Out-degree analysis

The highest out-degree node is Senator Lisa Murkowski @lisamurkowski. It means that Murkowski follows the highest number of other senators (31 out of 44).

Betweenness analysis

The highest betweenness node is Lisa Murkowski, with the highest number of times acting as a bridge along the shortest path between two other nodes. The higher the betweenness is, the more a senator is connected with others in both directions. In this case, it means that Murkowski has the highest following or followed connections with other senators on Twitter. She is the “Twitter hub” among the 45 U.S. senators.

Reciprocity analysis

The reciprocity of the network is 0.5423729. It means that approximately 54% of edges in the network are symmetrical. In other words, at least half of the outgoing edges following others also have been followed back among 45 senators. It determines how inter-connected the network is.

The network plot with the legend is shown below.

Twitter network among the US Senators and The Twitter community network plot

The algorithm has detected two communities among these senators. The computer-generated membership and the actual party affiliation are not exactly matched with each other, but as it is shown in the diagrams above, they have great similarities.

The 12 senators in the blue area of the community network polt above who are also represented as the left part of the dendrogram below are all Democrats. It illustrates that they are closely connected with each other on Twitter as senators from the same party.

The dendrogram of the Twitter community among 45 US senators

Other seven Democrat senators are classified by the algorithm as the members of another community with Republicans. It means that these seven senators were more connected with the Republican senators on Twitter than with other senators from the same party under the detection of the computer.

The community detection also indicates that the Republican senators are better connected than the Democrat senators, so that all the Republican senators could be detected into the same community.

R code1
#Create a Network
if (!require("igraph")) install.packages("igraph", repos="https://cran.cnr.berkeley.edu/", dependencies = TRUE)
library("igraph")
m <- read.csv("./ussenators_edges.csv")
m[] <- lapply(m,tolower) #convert twitter handles into lower case
m2 <- m[m$followed %in% m$ego,]
m2 <- m2[!duplicated(m2),] #remove redundant rows
m2 <- cbind(m2, weight=rep(1,946))
colnames(m2) <- c("to", "from","weight")
m2 <- as.matrix(m2)
m2 <- m2[,c("from", "to","weight")]

nodes2 <- read.csv("/Users/yejiandan/Downloads/ussenators_nodes.csv")
nodes2[] <- lapply(nodes2,tolower)
net <- graph_from_data_frame(m2,vertices = nodes2,directed = T)
net
2
#Find the number of the edges
nrow(m2)
#or
sum(degree(net,mode = c("in")))
#Find the highest in-degree node
sort(degree(net,mode = c("in")),decreasing = TRUE)
#Find the highest out-degree node
sort(degree(net,mode = c("out")),decreasing = TRUE)
#Find the highest betweenness node
sort(betweeness(net),decreasing = TRUE)
#Calculate the reciproty
reciproty(net, ignore.loops = TURE)
3
#Create a network plot with legend (by using plot.ly)
if (!require("plotly")) install.packages("plotly", repos="https://cran.cnr.berkeley.edu/", dependencies = TRUE)
library(plotly)
L_fr <- layout_with_fr(net)Xn <- L_fr[,1]
Yn <- L_fr[,2]
es <- as.data.frame(get.edgelist(net))
edge_shapes <- list() # Prepare a empty listfor(i in 1:length(E(net))) {
v0 <- es[i,]$V1
v1 <- es[i,]$V2

edge_shape <- list(
type = "line",
line = list(color = "#565656", width = 0.3),
x0 = Xn[v0],
y0 = Yn[v0],
x1 = Xn[v1],
y1 = Yn[v1]
)

edge_shapes[[i]] <- edge_shape
}
a <- list(
showarrow=TRUE,
arrowhead=3,
arrowsize=4,
arrowwidth=1.5,
arrowcolor='#636363'
)
network <- plot_ly()network <- add_markers(
network, x = ~Xn, y = ~Yn,
mode = "markers",
symbol = ~V(net)$Gender,
color = ~V(net)$party,
symbols = c("circle", "square"),
colors = c("blue", "red"),
marker = list(size = 12, annotations = a),
text = V(net)$name,
hoverinfo = "text")
axis <- list(title = "", showgrid = FALSE, showticklabels = FALSE, zeroline = FALSE) # Don't display axis and tick labelsp <- layout(network,
showlegend=TRUE,
title = 'Twitter Network among US Senators',
shapes = edge_shapes,
xaxis = axis,
yaxis = axis
)
p <- hide_colorbar(p) # Don't display color barp4#Plot the networknode_color <- function(a){
ifelse(a == "republican", "red", "blue")
}
node_shape <- function(b){
ifelse(b == "male", "square", "circle")
}
plot(net,vertex.size=9,layout=L_fr,edge.arrow.size=.1, edge.curved=0,vertex.color=node_color(V(net)$party), vertex.label.color='black',vertex.label.cex=.7,vertex.shape=node_shape(V(net)$Gender))#Plot the network communitywc <- walktrap.community(net)
V(net)$grp_member <- membership(wc)
# Create a dendrogramdendPlot(wc, mode="hclust")#Plot the network communityplot(wc, net, vertex.size=5, vertex.label.cex=.7,edge.arrow.size=.1, edge.curved=0,vertex.label.color='green')

--

--