The following sources have been used in preparation of this assignment:
Case Study: Krackhardt asked employees at Silicon Systems, a high-tech firm that was undergoing a union certification campaign, to whom they reported, with whom they were friends, and to whom they go to for advice. These relationships provide insight into the firm’s embedded social structure, which many believe plays a strong role in shaping opinion and opinion change. In a typical unionization campaign, non management workers submit union authorization cards, the NLRB grants the union’s request, notifies management, and the workers hold an election in two months time. Management typically tries to convince workers that unions are bad and that unions only want dues, while pro-union workers try to convince others to organize for a stronger bargaining position. Krackhardt finds that the friendship network is key to understanding the strong ties that shape collective decision-making. He states: “Someone, even an outsider, who understands the structure of philos (friendship) ties within an organization will be much more able to anticipate political resistance and facilitate change.”
We will only partially analyze Krackhardt’s data set in the following sections. There are a total of 29 steps. Some steps have been worked out for you but a dozen have been left for you to work out.
One of the purpose of this assignment is to pull together all that you have learned about R, sna, network and igraph. Recall, R is a interpretive runtime environment where you can enter and execute functions into the R console. We will be using R Studio that allows saving larger R programs in a file and executing all the R functions in that file one at a time or as a batch. The results of the execution are displayed in the R Console panel. Any line in the file that starts with a # is considered as a comment and ignored. R Studio also allows creation of R Notebook such as this one where you can interleave text with chunks of R code. These chunks can be executed all at once or one at a time. The output of executing each chunk is displayed right below that chunk. The final output of an R Notebook can be save either as an HTML file or a PDF file by configuring the settings in its header.We have it currently set as an html_document but when you are finished change it to a pdf_document and run the File > Knit Command to generate the PDF document to submit for this assignment. When having difficulty with any R command you can start with R’s built help functionality (for instance type ? followed by the name of a function you are having difficulty with in the R Console or use the help panel in R Studio). If during the execution you get an error on a missing package, use Tools > Install Packages from R Studio or executing the install function in the R console to install the missing package.
Step 1. Load the igraph package
library(igraph)
##
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
##
## decompose, spectrum
## The following object is masked from 'package:base':
##
## union
IMPORTANT NOTE 1: Sometimes, different packages overlap in functionality and cause unexpected behavior when both are loaded simultaneously. If you ever want to remove an existing library, use the “detach” command: detach(package:igraph)
IMPORTANT NOTE 2: Unlike in most languages, R objects are numbered from 1 instead of 0, so if you want the first element in a vector, you would reference it by vector_name[1]. However, igraph objects are numbered starting from 0. This can lead to lots of confusion, since it’s not always obvious at first which objects are native to R and which belong to igraph.
Step 2. Load the network data. You may need to set your working directory using the setwd() command. Also, pay attention to whether the data file includes column headers or not.
advice_data_frame <- read.csv('846 Lesson 4 Assignment EdgeList Advice.csv')
friendship_data_frame <- read.csv('846 Lesson 4 Assignment Edgelist Friendship.csv')
reports_to_data_frame <- read.csv('846 Lesson 4 Assignment Edgelist Reports To.csv')
attributes_data_frame <- read.csv('846 Lesson 4 Assignment Attributes.csv', header=T)
IMPORTANT NOTE: There are many flavors of read command to load data from many files in different formats. read.delim() is a general function for loading any delimited text file. The default is tab-delimited, but this can be overridden by setting the “sep” parameter. For example:
f <- read.delim("tab_delimited_file.txt")
f <- read.delim("colon_delimited_file.txt", sep=':')
Step 3. Examine the advice data you have loaded
advice_data_frame
## X1 X1.1 X0
## 1 1 2 1
## 2 1 3 0
## 3 1 4 1
## 4 1 5 0
## 5 1 6 0
## 6 1 7 0
## 7 1 8 1
## 8 1 9 0
## 9 1 10 0
## 10 1 11 0
## 11 1 12 0
## 12 1 13 0
## 13 1 14 0
## 14 1 15 0
## 15 1 16 1
## 16 1 17 0
## 17 1 18 1
## 18 1 19 0
## 19 1 20 0
## 20 1 21 1
## 21 2 1 0
## 22 2 2 0
## 23 2 3 0
## 24 2 4 0
## 25 2 5 0
## 26 2 6 1
## 27 2 7 1
## 28 2 8 0
## 29 2 9 0
## 30 2 10 0
## 31 2 11 0
## 32 2 12 0
## 33 2 13 0
## 34 2 14 0
## 35 2 15 0
## 36 2 16 0
## 37 2 17 0
## 38 2 18 0
## 39 2 19 0
## 40 2 20 0
## 41 2 21 1
## 42 3 1 1
## 43 3 2 1
## 44 3 3 0
## 45 3 4 1
## 46 3 5 0
## 47 3 6 1
## 48 3 7 1
## 49 3 8 1
## 50 3 9 1
## 51 3 10 1
## 52 3 11 1
## 53 3 12 1
## 54 3 13 0
## 55 3 14 1
## 56 3 15 0
## 57 3 16 0
## 58 3 17 1
## 59 3 18 1
## 60 3 19 0
## 61 3 20 1
## 62 3 21 1
## 63 4 1 1
## 64 4 2 1
## 65 4 3 0
## 66 4 4 0
## 67 4 5 0
## 68 4 6 1
## 69 4 7 0
## 70 4 8 1
## 71 4 9 0
## 72 4 10 1
## 73 4 11 1
## 74 4 12 1
## 75 4 13 0
## 76 4 14 0
## 77 4 15 0
## 78 4 16 1
## 79 4 17 1
## 80 4 18 1
## 81 4 19 0
## 82 4 20 1
## 83 4 21 1
## 84 5 1 1
## 85 5 2 1
## 86 5 3 0
## 87 5 4 0
## 88 5 5 0
## 89 5 6 1
## 90 5 7 1
## 91 5 8 1
## 92 5 9 0
## 93 5 10 1
## 94 5 11 1
## 95 5 12 0
## 96 5 13 1
## 97 5 14 1
## 98 5 15 0
## 99 5 16 1
## 100 5 17 1
## 101 5 18 1
## 102 5 19 1
## 103 5 20 1
## 104 5 21 1
## 105 6 1 0
## 106 6 2 0
## 107 6 3 0
## 108 6 4 0
## 109 6 5 0
## 110 6 6 0
## 111 6 7 0
## 112 6 8 0
## 113 6 9 0
## 114 6 10 0
## 115 6 11 0
## 116 6 12 0
## 117 6 13 0
## 118 6 14 0
## 119 6 15 0
## 120 6 16 0
## 121 6 17 0
## 122 6 18 0
## 123 6 19 0
## 124 6 20 0
## 125 6 21 1
## 126 7 1 0
## 127 7 2 1
## 128 7 3 0
## 129 7 4 0
## 130 7 5 0
## 131 7 6 1
## 132 7 7 0
## 133 7 8 0
## 134 7 9 0
## 135 7 10 0
## 136 7 11 1
## 137 7 12 1
## 138 7 13 0
## 139 7 14 1
## 140 7 15 0
## 141 7 16 0
## 142 7 17 1
## 143 7 18 1
## 144 7 19 0
## 145 7 20 0
## 146 7 21 1
## 147 8 1 0
## 148 8 2 1
## 149 8 3 0
## 150 8 4 1
## 151 8 5 0
## 152 8 6 1
## 153 8 7 1
## 154 8 8 0
## 155 8 9 0
## 156 8 10 1
## 157 8 11 1
## 158 8 12 0
## 159 8 13 0
## 160 8 14 0
## 161 8 15 0
## 162 8 16 0
## 163 8 17 0
## 164 8 18 1
## 165 8 19 0
## 166 8 20 0
## 167 8 21 1
## 168 9 1 1
## 169 9 2 1
## 170 9 3 0
## 171 9 4 0
## 172 9 5 0
## 173 9 6 1
## 174 9 7 1
## 175 9 8 1
## 176 9 9 0
## 177 9 10 1
## 178 9 11 1
## 179 9 12 1
## 180 9 13 0
## 181 9 14 1
## 182 9 15 0
## 183 9 16 1
## 184 9 17 1
## 185 9 18 1
## 186 9 19 0
## 187 9 20 0
## 188 9 21 1
## 189 10 1 1
## 190 10 2 1
## 191 10 3 1
## 192 10 4 1
## 193 10 5 1
## 194 10 6 0
## 195 10 7 0
## 196 10 8 1
## 197 10 9 0
## 198 10 10 0
## 199 10 11 1
## 200 10 12 0
## 201 10 13 1
## 202 10 14 0
## 203 10 15 1
## 204 10 16 1
## 205 10 17 1
## 206 10 18 1
## 207 10 19 1
## 208 10 20 1
## 209 10 21 0
## 210 11 1 1
## 211 11 2 1
## 212 11 3 0
## 213 11 4 0
## 214 11 5 0
## 215 11 6 0
## 216 11 7 1
## 217 11 8 0
## 218 11 9 0
## 219 11 10 0
## 220 11 11 0
## 221 11 12 0
## 222 11 13 0
## 223 11 14 0
## 224 11 15 0
## 225 11 16 0
## 226 11 17 0
## 227 11 18 0
## 228 11 19 0
## 229 11 20 0
## 230 11 21 0
## 231 12 1 0
## 232 12 2 0
## 233 12 3 0
## 234 12 4 0
## 235 12 5 0
## 236 12 6 0
## 237 12 7 1
## 238 12 8 0
## 239 12 9 0
## 240 12 10 0
## 241 12 11 0
## 242 12 12 0
## 243 12 13 0
## 244 12 14 0
## 245 12 15 0
## 246 12 16 0
## 247 12 17 0
## 248 12 18 0
## 249 12 19 0
## 250 12 20 0
## 251 12 21 1
## 252 13 1 1
## 253 13 2 1
## 254 13 3 0
## 255 13 4 0
## 256 13 5 1
## 257 13 6 0
## 258 13 7 0
## 259 13 8 0
## 260 13 9 1
## 261 13 10 0
## 262 13 11 0
## 263 13 12 0
## 264 13 13 0
## 265 13 14 1
## 266 13 15 0
## 267 13 16 0
## 268 13 17 0
## 269 13 18 1
## 270 13 19 0
## 271 13 20 0
## 272 13 21 0
## 273 14 1 0
## 274 14 2 1
## 275 14 3 0
## 276 14 4 0
## 277 14 5 0
## 278 14 6 0
## 279 14 7 1
## 280 14 8 0
## 281 14 9 0
## 282 14 10 0
## 283 14 11 0
## 284 14 12 0
## 285 14 13 0
## 286 14 14 0
## 287 14 15 0
## 288 14 16 0
## 289 14 17 0
## 290 14 18 1
## 291 14 19 0
## 292 14 20 0
## 293 14 21 1
## 294 15 1 1
## 295 15 2 1
## 296 15 3 1
## 297 15 4 1
## 298 15 5 1
## 299 15 6 1
## 300 15 7 1
## 301 15 8 1
## 302 15 9 1
## 303 15 10 1
## 304 15 11 1
## 305 15 12 1
## 306 15 13 1
## 307 15 14 1
## 308 15 15 0
## 309 15 16 1
## 310 15 17 1
## 311 15 18 1
## 312 15 19 1
## 313 15 20 1
## 314 15 21 1
## 315 16 1 1
## 316 16 2 1
## 317 16 3 0
## 318 16 4 0
## 319 16 5 0
## 320 16 6 0
## 321 16 7 0
## 322 16 8 0
## 323 16 9 0
## 324 16 10 1
## 325 16 11 0
## 326 16 12 0
## 327 16 13 0
## 328 16 14 0
## 329 16 15 0
## 330 16 16 0
## 331 16 17 0
## 332 16 18 1
## 333 16 19 0
## 334 16 20 0
## 335 16 21 0
## 336 17 1 1
## 337 17 2 1
## 338 17 3 0
## 339 17 4 1
## 340 17 5 0
## 341 17 6 0
## 342 17 7 1
## 343 17 8 0
## 344 17 9 0
## 345 17 10 0
## 346 17 11 0
## 347 17 12 0
## 348 17 13 0
## 349 17 14 0
## 350 17 15 0
## 351 17 16 0
## 352 17 17 0
## 353 17 18 0
## 354 17 19 0
## 355 17 20 0
## 356 17 21 1
## 357 18 1 1
## 358 18 2 1
## 359 18 3 1
## 360 18 4 1
## 361 18 5 1
## 362 18 6 0
## 363 18 7 1
## 364 18 8 1
## 365 18 9 1
## 366 18 10 1
## 367 18 11 1
## 368 18 12 0
## 369 18 13 1
## 370 18 14 1
## 371 18 15 1
## 372 18 16 1
## 373 18 17 0
## 374 18 18 0
## 375 18 19 1
## 376 18 20 1
## 377 18 21 1
## 378 19 1 1
## 379 19 2 1
## 380 19 3 1
## 381 19 4 0
## 382 19 5 1
## 383 19 6 0
## 384 19 7 1
## 385 19 8 0
## 386 19 9 0
## 387 19 10 1
## 388 19 11 1
## 389 19 12 0
## 390 19 13 0
## 391 19 14 1
## 392 19 15 1
## 393 19 16 0
## 394 19 17 0
## 395 19 18 1
## 396 19 19 0
## 397 19 20 1
## 398 19 21 0
## 399 20 1 1
## 400 20 2 1
## 401 20 3 0
## 402 20 4 0
## 403 20 5 0
## 404 20 6 1
## 405 20 7 0
## 406 20 8 1
## 407 20 9 0
## 408 20 10 0
## 409 20 11 1
## 410 20 12 1
## 411 20 13 0
## 412 20 14 1
## 413 20 15 1
## 414 20 16 1
## 415 20 17 1
## 416 20 18 1
## 417 20 19 0
## 418 20 20 0
## 419 20 21 1
## 420 21 1 0
## 421 21 2 1
## 422 21 3 1
## 423 21 4 1
## 424 21 5 0
## 425 21 6 1
## 426 21 7 1
## 427 21 8 1
## 428 21 9 0
## 429 21 10 0
## 430 21 11 0
## 431 21 12 1
## 432 21 13 0
## 433 21 14 1
## 434 21 15 0
## 435 21 16 0
## 436 21 17 1
## 437 21 18 1
## 438 21 19 0
## 439 21 20 1
## 440 21 21 0
Step 4. Since friendship data is a bit long, just examine the top few rows
head(friendship_data_frame)
## X1 X1.1 X0
## 1 1 2 1
## 2 1 3 0
## 3 1 4 1
## 4 1 5 0
## 5 1 6 0
## 6 1 7 0
Step 5. Since reports to data is a bit long, just examine the bottom rows of reports data
tail(reports_to_data_frame)
## X1 X1.1 X0
## 435 21 16 0
## 436 21 17 0
## 437 21 18 0
## 438 21 19 0
## 439 21 20 0
## 440 21 21 0
IMPORTANT NOTE: If you want view your data in a separate window like a spreadsheet , you can use the command ‘fix()’.
Step 6. Try this with the attributes data. You will need to close the window before R moves on to the next function.
fix(attributes_data_frame)
Step 7. Assign column names to all the data frames.
colnames(advice_data_frame) <- c('ego', 'alter', 'advice_tie')
colnames(friendship_data_frame) <- c('ego', 'alter', 'friendship_tie')
colnames(reports_to_data_frame) <- c('ego', 'alter', 'reports_to_tie')
Step 8. Take a look at each data frame using the ’fix()" function. Note that you’ll need to close each fix window before R will evaluate the next line of code.
fix(advice_data_frame)
fix(friendship_data_frame)
fix(reports_to_data_frame)
Step 9. The next step is to merge these data sets but before we do that, let us make sure the ‘ego’ and ‘alter’ are the same across all data sets.
advice_data_frame$ego == friendship_data_frame$ego # should return TRUE
## [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [15] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [29] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [43] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [57] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [71] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [85] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [99] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [113] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [127] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [141] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [155] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [169] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [183] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [197] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [211] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [225] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [239] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [253] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [267] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [281] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [295] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [309] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [323] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [337] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [351] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [365] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [379] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [393] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [407] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [421] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [435] TRUE TRUE TRUE TRUE TRUE TRUE
Step 10. The output of the previous step is quite verbose. An alternative is to check if there are rows with mismatching ‘ego’ and alter.’
which(advice_data_frame$ego != friendship_data_frame$ego)
## integer(0)
Step 11. Repeat this for other variables.
which(advice_data_frame$alter != friendship_data_frame$alter)
## integer(0)
which(reports_to_data_frame$alter != friendship_data_frame$alter)
## integer(0)
which(reports_to_data_frame$ego != friendship_data_frame$ego)
## integer(0)
Step 12. Once verified, merge the three data sets into a single frame.
full_data_frame <- cbind(advice_data_frame, friendship_data_frame$friendship_tie, reports_to_data_frame$reports_to_tie)
head(full_data_frame)
## ego alter advice_tie friendship_data_frame$friendship_tie
## 1 1 2 1 1
## 2 1 3 0 0
## 3 1 4 1 1
## 4 1 5 0 0
## 5 1 6 0 0
## 6 1 7 0 0
## reports_to_data_frame$reports_to_tie
## 1 1
## 2 0
## 3 0
## 4 0
## 5 0
## 6 0
Step 13. Simplify the names of the last two columns ‘friendship_data_frame\(friendship_tie' and 'reports_to_data_frame\)reports_to_tie’ to ‘friendship_tie’ and ‘reports_to_tie’ respectively.
names(full_data_frame)[4:5] <- c("friendship_tie", "reports_to_tie")
head(full_data_frame)
## ego alter advice_tie friendship_tie reports_to_tie
## 1 1 2 1 1 1
## 2 1 3 0 0 0
## 3 1 4 1 1 0
## 4 1 5 0 0 0
## 5 1 6 0 0 0
## 6 1 7 0 0 0
Step 14. Using the subset() command, subset full_data_frame such that it only contains only those rows that have a non-zero entry for any of the three types of edges. Store the result in full_nonzero_edges
full_nonzero_edges <- subset(full_data_frame, (advice_tie > 0 | friendship_tie > 0 | reports_to_tie > 0))
head(full_nonzero_edges)
## ego alter advice_tie friendship_tie reports_to_tie
## 1 1 2 1 1 1
## 3 1 4 1 1 0
## 7 1 8 1 1 0
## 11 1 12 0 1 0
## 15 1 16 1 1 0
## 17 1 18 1 0 0
Step 15. Create a graph from the subset of data obtained in step 13 using igraph package’s graph.data.frame() function and examine the summary of the created graph using the summary() command. - I think it is supposed to be step 14 since a subset is used in step 14-
g <- graph.data.frame(full_nonzero_edges)
summary(g)
## IGRAPH 9a13e96 DN-- 21 232 --
## + attr: name (v/c), advice_tie (e/n), friendship_tie (e/n),
## | reports_to_tie (e/n)
Step 16. The command graph.data.frame() treats the first two columns of a data frame as an edge and the reamining columns as the attributes of the edge. In the summary, therefore, there are 232 edges joined by one of three different types of ties. Use the get.edge.attribute() command to obtain a vector of edges of a specific type of tie.
get.edge.attribute(g)
## $advice_tie
## [1] 1 1 1 0 1 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1
## [36] 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 1 1
## [71] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1
## [106] 1 1 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 1 1 1 1 1 0 1 1 1 1 0 1 1 1 1
## [141] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 0 1 0 0 0 0 0 0
## [176] 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1
## [211] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
##
## $friendship_tie
## [1] 1 1 1 1 1 0 0 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 1 1 0 1 0 0 1
## [36] 1 1 0 0 0 0 1 0 0 0 1 0 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0
## [71] 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 1 0 1 0 0 1 0 0
## [106] 0 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 0 0 1 0 1 0 0 0 1 1 0 0 1 0
## [141] 1 0 1 1 0 0 1 0 1 0 0 1 0 0 0 1 0 0 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1
## [176] 1 1 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 1 0
## [211] 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 1 1 0
##
## $reports_to_tie
## [1] 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0
## [36] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
## [71] 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
## [106] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0
## [141] 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
## [176] 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
## [211] 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
Step 17. Make the ties symmetric by converting the graph into an undirected graph using the command as.undirected().
as.undirected(g)
## IGRAPH 9a1ff8f UN-- 21 159 --
## + attr: name (v/c)
## + edges from 9a1ff8f (vertex names):
## [1] 1 --2 1 --3 2 --3 1 --4 2 --4 3 --4 1 --5 2 --5 2 --6 3 --6
## [11] 4 --6 5 --6 2 --7 3 --7 5 --7 6 --7 1 --8 2 --8 3 --8 4 --8
## [21] 5 --8 6 --8 7 --8 1 --9 2 --9 3 --9 5 --9 6 --9 7 --9 8 --9
## [31] 1 --10 2 --10 3 --10 4 --10 5 --10 8 --10 9 --10 1 --11 2 --11 3 --11
## [41] 4 --11 5 --11 7 --11 8 --11 9 --11 10--11 1 --12 3 --12 4 --12 6 --12
## [51] 7 --12 9 --12 10--12 11--12 1 --13 2 --13 5 --13 9 --13 10--13 11--13
## [61] 2 --14 3 --14 5 --14 7 --14 9 --14 13--14 1 --15 2 --15 3 --15 4 --15
## [71] 5 --15 6 --15 7 --15 8 --15 9 --15 10--15 11--15 12--15 13--15 14--15
## + ... omitted several edges
Step 18. First create a vector of vertex labels, in this case 1:n
attributes = cbind(1:length(attributes_data_frame[,1]), attributes_data_frame)
attributes
## 1:length(attributes_data_frame[, 1]) AGE TENURE LEVEL DEPT
## 1 1 33 9.333 3 4
## 2 2 42 19.583 2 4
## 3 3 40 12.750 3 2
## 4 4 33 7.500 3 4
## 5 5 32 3.333 3 2
## 6 6 59 28.000 3 1
## 7 7 55 30.000 1 0
## 8 8 34 11.333 3 1
## 9 9 62 5.417 3 2
## 10 10 37 9.250 3 3
## 11 11 46 27.000 3 3
## 12 12 34 8.917 3 1
## 13 13 48 0.250 3 2
## 14 14 43 10.417 2 2
## 15 15 40 8.417 3 2
## 16 16 27 4.667 3 4
## 17 17 30 12.417 3 1
## 18 18 33 9.083 2 3
## 19 19 32 4.833 3 2
## 20 20 38 11.667 3 2
## 21 21 36 12.500 2 1
Step 19. # An easy way to set attributes, is to read them in while creating the graph object.
g <- graph.data.frame(d = full_nonzero_edges, vertices = attributes)
summary(g)
## IGRAPH 9a2ceb5 DN-- 21 232 --
## + attr: name (v/c), AGE (v/n), TENURE (v/n), LEVEL (v/n), DEPT
## | (v/n), advice_tie (e/n), friendship_tie (e/n), reports_to_tie
## | (e/n)
Step 20. List of the values for a given attribute for all of the actors in the network.
vertex.attributes(g)
## $name
## [1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14"
## [15] "15" "16" "17" "18" "19" "20" "21"
##
## $AGE
## [1] 33 42 40 33 32 59 55 34 62 37 46 34 48 43 40 27 30 33 32 38 36
##
## $TENURE
## [1] 9.333 19.583 12.750 7.500 3.333 28.000 30.000 11.333 5.417 9.250
## [11] 27.000 8.917 0.250 10.417 8.417 4.667 12.417 9.083 4.833 11.667
## [21] 12.500
##
## $LEVEL
## [1] 3 2 3 3 3 3 1 3 3 3 3 3 3 2 3 3 3 2 3 3 2
##
## $DEPT
## [1] 4 4 2 4 2 1 0 1 2 3 3 1 2 2 2 4 1 3 2 2 1
Step 21. Use R’s general-purpose plot() method to visualize the network g.
plot(g, edge.arrow.size=.25, vertex.label.cex=0.6)
Step 22. Since this network looks a bit complex, plot the networks for single edge types. Start with the ‘advice_tie.’
plot(g, edge.label=E(g)$advice_tie, edge.arrow.size=.25, vertex.label.cex=0.6)
Step 23. Plot the networks for ‘friendship_tie.’
plot(g, edge.label=E(g)$friendship_tie, edge.arrow.size=.25, vertex.label.cex=0.6)
Step 24. Plot the networks for ‘reports_to_tie.’
plot(g, edge.label=E(g)$reports_to_tie, edge.arrow.size=.25, vertex.label.cex=0.6)
Step 25. Plot the networks for ‘reports_to_tie’ using Kamada-Kawai layout; other options for igraph layout can be accessed by entering ?layout.
reports_to_layout <- layout.kamada.kawai(g)
#plot(reports_to_tie, edge.arrow.size=.25, vertex.label.cex=0.6, layout=reports_to_layout)
#reports_to_layout <- layout.kamada.kawai(g_reports_to_only)
#plot(g_reports_to_only, edge.arrow.size=.25, vertex.label.cex=0.6, layout=reports_to_layout)
Step 26. Color-code vertices by department and clean up the plot by removing vertex labels and shrinking the arrow size.
dept_vertex_colors = igraph::get.vertex.attribute(g,"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]
plot(g, layout=reports_to_layout, vertex.color=dept_vertex_colors, vertex.label=NA, edge.arrow.size=.5)
Step 27. Plot g_reports_to_tie after setting the vertex size by tenure
tenure_vertex_colors = igraph::get.vertex.attribute(g,"TENURE")
colors = c('Black', 'Red', 'Blue', 'Yellow', 'Green')
tenure_vertex_colors[tenure_vertex_colors == 0] = colors[1]
tenure_vertex_colors[tenure_vertex_colors == 1] = colors[2]
tenure_vertex_colors[tenure_vertex_colors == 2] = colors[3]
tenure_vertex_colors[tenure_vertex_colors == 3] = colors[4]
tenure_vertex_colors[tenure_vertex_colors == 4] = colors[5]
plot(g, layout=reports_to_layout, vertex.color=tenure_vertex_colors, vertex.label=NA, edge.arrow.size=.5)
Step 28. incorporate additional tie types. Use the layout generated by the reports-to ties but overlay the advice and friendship ties in red and blue. Add a legend for the different tie types.
tie_type_colors = c(rgb(1,0,0,.5), rgb(0,0,1,.5), rgb(0,0,0,.5))
E(g)$color[ E(g)$advice_tie==1 ] = tie_type_colors[1]
E(g)$color[ E(g)$friendship_tie==1 ] = tie_type_colors[2]
E(g)$color[ E(g)$reports_to_tie==1 ] = tie_type_colors[3]
E(g)$arrow.size=.5
V(g)$color = dept_vertex_colors
V(g)$frame = dept_vertex_colors
plot(g, layout=reports_to_layout, vertex.color=dept_vertex_colors, vertex.label=NA, edge.arrow.size=.5)
#,vertex.size=tenure_vertex_sizes
legend(1, 1.25, legend = c('Advice', 'Friendship', 'Reports To'), col = tie_type_colors, lty=1, cex = .7)
Step 29. Overlay the edges from one tie type on the structure generated by another tie type. Here we can use the reports_to_layout but show the friendship ties.
# Figure out