The original address of the article is from: https://sna.stanford.edu/lab.php?l=1
Introduction Laboratory
This experiment focuses on introducing two packages of SNA and Igraph to students to cover some basic R commands, load and manage data, generate graphical visualization, and export data for use elsewhere.
The address of all packages that need to be installed is as follows. It is recommended to install them directly.
source("http://sna.stanford.edu/setup.R")
install.packages("ergm", repos = "http://cran.cnr.berkeley.edu/", dependencies = TRUE)
install.packages("reshape", repos = "http://cran.cnr.berkeley.edu/", dependencies = TRUE)
install.packages("igraph", repos = "http://cran.cnr.berkeley.edu/", dependencies = TRUE)
install.packages("sna", repos = "http://cran.cnr.berkeley.edu/", dependencies = TRUE)
install.packages("numDeriv", repos = "http://cran.cnr.berkeley.edu/", dependencies = TRUE)
install.packages("MatchIt", repos = "http://cran.cnr.berkeley.edu/", dependencies = TRUE)
install.packages("coin", repos = "http://cran.cnr.berkeley.edu/", dependencies = TRUE)
install.packages("boot", repos = "http://cran.cnr.berkeley.edu/", dependencies = TRUE)
install.packages("Hmisc", repos = "http://cran.cnr.berkeley.edu/", dependencies = TRUE)
install.packages("lattice", repos = "http://cran.cnr.berkeley.edu/", dependencies = TRUE)
install.packages("triads", repos = "http://cran.cnr.berkeley.edu/", dependencies = TRUE)
install.packages("psych", repos = "http://cran.cnr.berkeley.edu/", dependencies = TRUE)
install.packages("nFactors", repos = "http://cran.cnr.berkeley.edu/", dependencies = TRUE)
install.packages("animation", repos = "http://cran.cnr.berkeley.edu/", dependencies = TRUE)
install.packages("NetData", repos = "http://cran.cnr.berkeley.edu/", dependencies = TRUE)
install.packages("NetCluster", repos = "http://cran.cnr.berkeley.edu/", dependencies = TRUE)
install.packages("igraphtosonia", repos = "http://cran.cnr.berkeley.edu/", dependencies = TRUE)
The first experiment requires loading packages
library(igraph)
If you need to delete this package, please execute
detach(package:igraph)
The first step is to import data
Use this function read.table()
advice_data_frame <- read.table('http://sna.stanford.edu/sna_R_labs/data/Krack-High-Tec-edgelist-Advice.txt')
friendship_data_frame <- read.table('http://sna.stanford.edu/sna_R_labs/data/Krack-High-Tec-edgelist-Friendship.txt')
reports_to_data_frame <- read.table('http://sna.stanford.edu/sna_R_labs/data/Krack-High-Tec-edgelist-ReportsTo.txt')
If your data is a file, put it somewhere, you can do this.
setwd('path/to/your_directory')
your_data_frame <- read.table('your_file_name')
Look at the first six rows of the table
head(friendship_data_frame)
Or the last six lines
tail(reports_to_data_frame)
Or open it in tabular form
fix(reports_to_data_frame)
How to read csv format table
attributes <- read.csv('http://sna.stanford.edu/sna_R_labs/data/Krack-High-Tec-Attributes.csv', header=T)
attributes
How to open a file that separates text and achieve the goal by setting SEP
f <- read.delim("tab_delimited_file.txt")
f <- read.delim("colon_delimited_file.txt", sep=':')
Similarly read.spss() reads spss files
STATA files via read.dta().
When data comes directly from a file package, data(kracknets, package = NetData)
Step 2 Load data
Replace the title with another name
colnames(advice_data_frame) <- c('ego', 'alter', 'advice_tie')
head(advice_data_frame)
colnames(friendship_data_frame) <- c('ego', 'alter', 'friendship_tie')
head(friendship_data_frame)
colnames(reports_to_data_frame) <- c('ego', 'alter', 'reports_to_tie')
head(reports_to_data_frame)
fix(advice_data_frame)
fix(friendship_data_frame)
fix(reports_to_data_frame)
Verify that the fields with the same parameters in each table are consistent
advice_data_frame$ego == friendship_data_frame$ego
which(advice_data_frame$ego != friendship_data_frame$ego)
which(advice_data_frame$alter != friendship_data_frame$alter)
which(reports_to_data_frame$alter != friendship_data_frame$alter)
which(reports_to_data_frame$ego != friendship_data_frame$ego)
Merge into a table
krack_full_data_frame <- cbind(advice_data_frame,
friendship_data_frame$friendship_tie,
reports_to_data_frame$reports_to_tie)
head(krack_full_data_frame)
names(krack_full_data_frame)[4:5] <- c("friendship_tie",
"reports_to_tie")
head(krack_full_data_frame)
Another way to merge
krack_full_data_frame <- data.frame(ego = advice_data_frame[,1],
alter = advice_data_frame[,2],
advice_tie = advice_data_frame[,3],
friendship_tie = friendship_data_frame[,3],
reports_to_tie = reports_to_data_frame[,3])
head(krack_full_data_frame)
Remove data without common edges from the data set directly (at least one is greater than 0)
Logical operations:
& amp;! (with, or not. )
krack_full_nonzero_edges <- subset(krack_full_data_frame,
(advice_tie > 0 | friendship_tie > 0 | reports_to_tie > 0))
head(krack_full_nonzero_edges)
Import the data into the graph graph chart. By default, the first two columns are the nodes of the graph, and the latter parameters are the attributes of the edge.
krack_full <- graph.data.frame(krack_full_nonzero_edges)
summary(krack_full)
The get.edge.attribute function is used to get the edge properties, but it's actually the last column of data.
It's just extracted separately.
get.edge.attribute(krack_full, 'advice_tie')
get.edge.attribute(krack_full, 'friendship_tie')
get.edge.attribute(krack_full, 'reports_to_tie')
as.undirected() is changed into undirected graph, and asymmetric graph is transformed into symmetric graph.
krack_full_symmetrized <- as.undirected(krack_full, mode='collapse')
summary(krack_full_symmetrized)
The third part adds vertex information to the graph
iterate
One way to add attributes to graphical objects is to iterate through each attribute and vertex. This means that we add an attribute to every vertex in the network at a time.
for (i in V(krack_full)) {
for (j in names(attributes)) {
krack_full <- set.vertex.attribute(krack_full,
j,
index = i,
attributes[i + 1, j])
}
}
Read-only property name
attributes = cbind(1:length(attributes[,1]), attributes)krack_full <- graph.data.frame(d = krack_full_nonzero_edges,
vertices = attributes)
summary(krack_full)
Point information
get.vertex.attribute(krack_full, 'AGE')
get.vertex.attribute(krack_full, 'TENURE')
get.vertex.attribute(krack_full, 'LEVEL')
get.vertex.attribute(krack_full, 'DEPT')
Part IV. Visual Network Diagram
Draw a simple graph and set the storage location of the graph.
setwd("")
pdf("1.1_Krackhardt_Full.pdf")
plot(krack_full)
dev.off()
Drawing undirected graph of single factor
advice only
krack_advice_only <- delete.edges(krack_full,
E(krack_full)[get.edge.attribute(krack_full,
name = "advice_tie") == 0])
summary(krack_advice_only)
pdf("1.2_Krackhardt_Advice.pdf")
plot(krack_advice_only)
dev.off()
Empathy
friendship only
krack_friendship_only <- delete.edges(krack_full,
E(krack_full)[get.edge.attribute(krack_full,
name = "friendship_tie") == 0])
summary(krack_friendship_only)
pdf("1.3_Krackhardt_Friendship.pdf")
plot(krack_friendship_only)
dev.off()
reports-to only
Empathy
krack_reports_to_only <- delete.edges(krack_full,
E(krack_full)[get.edge.attribute(krack_full,
name = "reports_to_tie") == 0])
summary(krack_reports_to_only)
pdf("1.4_Krackhardt_Reports.pdf")
plot(krack_reports_to_only)
dev.off()
*****the layout algorithm
Key Contents of chterman-Rheingold***
reports_to_layout <- layout.fruchterman.reingold(krack_reports_to_only)
pdf("1.5_Krackhardt_Reports_Fruchterman_Reingold.pdf")
plot(krack_reports_to_only,
layout=reports_to_layout)
dev.off()
Now let's color code the vertices by department and clean them up.
Draw the plot by removing the vertex label and reducing the size of the arrow.
dept_vertex_colors = get.vertex.attribute(krack_full,"DEPT")
colors = c('Black', 'Red', 'Blue', 'Yellow', 'Green')
dept_vertex_colors[dept_vertex_colors == 0] = colors[1]
dept_vertex_colors[dept_vertex_colors == 1] = colors[2]
dept_vertex_colors[dept_vertex_colors == 2] = colors[3]
dept_vertex_colors[dept_vertex_colors == 3] = colors[4]
dept_vertex_colors[dept_vertex_colors == 4] = colors[5]
pdf("1.6_Krackhardt_Reports_Color.pdf")
plot(krack_reports_to_only,
layout=reports_to_layout,
vertex.color=dept_vertex_colors,
vertex.label=NA,
edge.arrow.size=.5)
dev.off()
Set the size of the vertex
tenure_vertex_sizes = get.vertex.attribute(krack_full,"TENURE")
pdf("1.7_Krackhardt_Reports_Vertex_Size.pdf")
plot(krack_reports_to_only,
layout=reports_to_layout,
vertex.color=dept_vertex_colors,
vertex.label=NA,
edge.arrow.size=.5,
vertex.size=tenure_vertex_sizes)
dev.off()
advice and friendship ties in red and blue.
Adding color to the other two properties
tie_type_colors = c(rgb(1,0,0,.5), rgb(0,0,1,.5), rgb(0,0,0,.5))
E(krack_full)$color[ E(krack_full)$advice_tie==1 ] = tie_type_colors[1]
E(krack_full)$color[ E(krack_full)$friendship_tie==1 ] = tie_type_colors[2]
E(krack_full)$color[ E(krack_full)$reports_to_tie==1 ] = tie_type_colors[3]
E(krack_full)$arrow.size=.5
V(krack_full)$color = dept_vertex_colors
V(krack_full)$frame = dept_vertex_colors
pdf("1.8_Krackhardt_Overlayed_Ties.pdf")
plot(krack_full,
layout=reports_to_layout,
vertex.color=dept_vertex_colors,
vertex.label=NA,
edge.arrow.size=.5,
vertex.size=tenure_vertex_sizes)
Add legend
legend(1,
1.25,
legend = c('Advice',
'Friendship',
'Reports To'),
col = tie_type_colors,
lty=1,
cex = .7)
dev.off()
Another way to display data structure layout and final graphical data is to exchange a reference standard
pdf("1.9_Krackhardt_Overlayed_Structure.pdf")
plot(krack_friendship_only,
layout=reports_to_layout,
vertex.color=dept_vertex_colors,
vertex.label=NA,
edge.arrow.size=.5,
vertex.size=tenure_vertex_sizes,
main='Krackhardt High-Tech Managers')
dev.off()
Part 5: Exporting the data of this network graph
write.graph(krack_full, file='krack_full.dl', format="pajek")
write.graph(krack_full, file='krack_full.txt', format="edgelist")