# Loading the packages needed for work
library(igraph)
library(statnet)
library(tidygraph)
library(intergraph)
library(dplyr)
library(openxlsx)
# Importing data from a CSV file
edges_5000 <- read.csv("retweets_political_5000.csv")
# Selecting the columns needed for work
edges_5000_1 <- edges_5000 %>%
select(source, target) # Creating a network object from tabular data
tg_5000 <- as_tbl_graph(edges_5000_1) %>%
activate(nodes) %>% # Activate the nodes table
activate(edges) # Activate the edges table
# Switching to the statnet format for further network analysis
tg_5000 <- intergraph::asNetwork(tg_5000)
# Checking the class of the studied object
class(tg_5000)
# Computing descriptive network statistics, including network density
summary(tg_5000) # Computing degree and betweenness centrality for each vertex
deg <- degree(tg_5000)
bet <- betweenness(tg_5000)
# Combining the centrality values into a table
df_tg_5000 <- data.frame(deg, bet)
# Switching to igraph format for further network analysis
net_to_graph_5000 <- asIgraph(tg_5000)
# Extracting vertex names and writing them into a separate variable
nodes_name <- vertex_attr(net_to_graph_5000,
name = "vertex.names", index = V(net_to_graph_5000))
# Adding the variable to the previously created table
df_tg_5000 <- add_column(df_tg_5000, nodes_name, .before = 1)
# Saving the resulting table to the working folder as .xlsx
write.xlsx(df_tg_5000, 'df_tg_5000.xlsx') # Sorting by descending deg values;
# Filtering by the condition: deg >= 40;
# Selecting only columns containing deg values and node names
deg_1 <- df_tg_5000 %>%
arrange(desc(deg)) %>%
filter(deg >= 40) %>%
select(nodes_name, deg)
# Saving the resulting table to the working folder as .xlsx
write.xlsx(deg_1, 'deg_1.xlsx')
# Sorting by descending bet values;
# Filtering by the condition: bet >= 10000;
# Selecting only columns containing bet values and node names
bet_1 <- df_tg_5000 %>%
arrange(desc(bet)) %>%
filter(bet >= 10000) %>%
select(nodes_name, bet)
# Saving the resulting table to the working folder as .xlsx
write.xlsx(bet_1, 'bet_1.xlsx')
# Visualizing network data
plot(net_to_graph_5000, vertex.label = NA)
# Saving a network layout option into a separate variable
lo <- layout_with_kk(net_to_graph_5000)
# Setting up network visualization
plot(net_to_graph_5000,
vertex.size= log(deg), # Node size proportional to log(degree)
vertex.label = NA, # Node names will not be displayed
vertex.color = deg, # Node color tied to degree centrality
edge.arrow.size = .25, # Edge arrow size
layout=lo*1) # Kamada–Kawai layout minimizes edge crossings
# Filtering the network based on degree centrality values >= 40
filtr_net_to_graph_5000_deg <- get.inducedSubgraph(tg_5000, which(deg >= 40))
# Switching to igraph format for further analysis
net_to_graph_5000_deg <- asIgraph(filtr_net_to_graph_5000_deg)
# Saving a layout option for the filtered network
lo_deg <- layout_with_kk(net_to_graph_5000_deg)
# Loading the table with vertex attributes
deg_1_at <- read.xlsx("deg_1_at.xlsx")
# Visualizing the filtered network
my_pal <- brewer.pal(5, "Dark2")
rolecat <- as.factor(deg_1_at$type)
plot(net_to_graph_5000_deg, vertex.size= log(deg),
edge.arrow.width = .25,
edge.arrow.size = .25,
layout = lo_deg*1,
vertex.color = my_pal[rolecat],
vertex.label = deg_1_at[,3],
asp = 0.35)
# Determining the k-core structure of the network
coreness_5000 <- graph.coreness(net_to_graph_5000)
table(coreness_5000)
maxcoreness <- max(coreness_5000)
# Assigning labels, selecting colors, and visualizing the k-core structure
Vname <- vertex_attr(net_to_graph_5000, name = "vertex.names",
index = V(net_to_graph_5000))
V(net_to_graph_5000)$name <- Vname
V(net_to_graph_5000)$color <- coreness_5000
plot(net_to_graph_5000, vertex.label = NA,
edge.arrow.width = 0.25,
edge.arrow.size = 0.25, layout=lo*1,
vertex.size = 1.0, asp = 0.35)
# Removing k-cores that do not meet the specified condition
net_to_graph_5000_10_22 <- induced.subgraph(net_to_graph_5000,
vids = which(coreness_5000 >= 10))
# Visualizing the filtering result
V(net_to_graph_5000)$color <- coreness_5000
V(net_to_graph_5000)$name <- coreness_5000
plot(net_to_graph_5000_10_22, layout = lo[which(coreness_5000 >= 10),],
edge.arrow.width = 0.25,
edge.arrow.size = 0.1, asp = 0.35,
vertex.size= 3.0, vertex.label = deg_1_at[,3],
vertex.label.cex = 0.6,
main = "Graphical display of the filtered k-core network structure")