community membership of each node can be queried as follows:
friend_comm$membership
Visualize the network with community memberships
plot(friend_comm, krack_friendship_only)
communities are colored in different shades, and nodes within the same communities are clustered together
This process also lends itself to visualization as a dendrogram.
The y-axis reflects the distance metric used by the edge betweenness algorithm
plot(as.dendrogram(friend_comm))
the tree branches show membership hierarchies. nodes belonging to the same community share common roots
the y-axis is a distance measure showing how far apart are nodes
now we can rearrange nodes by their community memberships
first create a data frame that records node id and communities
and reorder node id's by membership affiliation
membership <- data.frame(cbind(friend_commmembership,friendcommnames))
names(membership) <- c('group','id')
class(membershipgroup). # string var
membershipgroup <- as.numeric(membershipgroup)membership<−membership[order(membershipgroup),] # sort data frame by the group column
we plot the matrix using color tiles, each tile represent the friendship relations btw two nodes.
nodes with friendship ties are colored as red, otherwise left blank
baseline plot
ggplot(krack_friend_long[krack_friend_long$tie>0,], aes(x = from, y = to, fill = factor(tie))) +
geom_raster() + # create rectangular tiles when all the tiles are the same size. can also use geom_tile()
labs(x='', y='', fill='Friend') +
scale_fill_manual(values=c('red','grey90'), # manually define colors that correspond to the levels in fill
breaks=c(1,0), # how to order legend for fill
labels=c('yes','no')) + # legend label. here we only have "yes"
theme_bw() +
theme(aspect.ratio = 1) # Force the plot into a square aspect ratio
how do we reorder the graph based on community affiliations?
recall we have membership affiliations from before:
membership
Reorder edge list "from" and "to" factor levels based on the ids ordered by membership affiliations
in factor(), arg "levels" can specify how we want the factor levels to be ordered
include membership info of from and to nodes using join function.
first change the names of the membership columns to be matched with plot_data
names(membership) <- c('from_group','from') # memebership of ego
plot_data <- krack_friend_long %>% plyr::join(membership, by='from') # plyr::join calls the join function from plyr library
names(membership) <- c('to_group','to') # memebership of alter
plot_data <- plot_data %>% plyr::join(membership, by='to')
names(plot_data) # membership info added
adding membership affiliations of nodes allow us to color in-group and out-group ties separately
here we want to color in-group ties by communities, and out-group ties as grey
how do we do that?
first we identify ties between members of the same community for in-group ties: from_group = to_group
if from_group != to_group, then the tie is an out-group tie
the info is stored in memb_tie variable
plot_datamemb_tie <- 0 # default is out-group
plot_datamemb_tie[plot_datafromgroup==plotdatato_group] <- 1 # mark out within-group ties
plot_datamembtie[plotdatamemb_tie==1] <- plot_datafromgroup[plotdatamemb_tie==1] # replace the marker with group info
plot_data$memb_tie
Now run the ggplot code again, include ties with non-zero values and color ties based on membership
within-group ties have different colors, while between-group ties are all grey
improved plot
ggplot(plot_data[plot_data$tie>0,], aes(x = from, y = to, fill = factor(memb_tie))) +
geom_raster() +
labs(x='', y='', fill='Within-community ties') +
scale_fill_manual(values = c('grey','magenta','darkgreen','royalblue')) + # manually set community colors
scale_x_discrete(drop = FALSE) +
scale_y_discrete(drop = FALSE) +
theme_bw() +
theme(aspect.ratio = 1)
nodes of the same communities are placed close to each other,
and we can now see some group structure from this adjacency matrix plot.
between-group ties are colored as grey, and within-group ties are colored according to membership affiliations
the advantage of adjacency matrix representation is that:
a) scalable, less messy than node-link diagrams when the number of nodes is large
Task 1. visualize all network ties in krack_full using the adjacency matrix.
color edges according to friendship, advice, and reports-to ties
Task 2. cluster nodes by community membership using the friendship network, and replot the adjacency matrix
with nodes belonging to the same communities placed close to each other.
construct a new variable, tie strength, which is the combination of friendship, advice, and reports-to ties
this counts the types of relationships between two nodes
Task 3. use radial tree to visualize the reports-to network, and a treemap to show the advice network.
in the treemap, set size proportional to ego's degrees from the advice and reports-to networks
Based on the walkthrough provided, let's address the tasks from the lab assignment:
Task 1: Visualize All Ties in krack_full using the Adjacency Matrix
To visualize all network ties in krack_full using an adjacency matrix and color edges according to friendship, advice, and reports-to ties, we combine all ties into one attribute and then plot this combined attribute.
# Combining all ties into one attribute
krack_full_combined <- as.matrix(as_adjacency_matrix(krack_full, attr=NULL))
krack_full_combined[krack_full_combined > 1] <- 1 # convert matrix to binary
# Converting the matrix into a long dataframe for ggplot
krack_full_long <- melt(krack_full_combined)
names(krack_full_long) <- c('from', 'to', 'tie')
krack_full_long$from <- factor(krack_full_long$from)
krack_full_long$to <- factor(krack_full_long$to)
# Create the adjacency matrix plot using ggplot2
ggplot(krack_full_long[krack_full_long$tie>0,], aes(x = from, y = to, fill = factor(tie))) +
geom_raster() +
labs(x='', y='', fill='Tie') +
scale_fill_manual(values=c('red','grey90'), breaks=c(1,0), labels=c('yes','no')) +
theme_bw() +
theme(aspect.ratio = 1)
Task 2: Cluster Nodes by Community Membership and Replot the Adjacency Matrix
For this task, we'll use similarly to previous steps to cluster nodes by their community memberships using the friendship network, and then replot the adjacency matrix such that nodes from the same community are placed closer.
First, clustering by community membership was previously performed:
Following this, you can adapt the previous steps for plotting an adjacency matrix, adjusting the plot data to reorder nodes based on this community membership, as you did before.
Task 3: Radial Tree and Treemap for Reports-to and Advice Networks
Radial Tree for Reports-to Network
# This requires you to adapt the previously outlined steps to specifically work with the 'reports-to' network.
# Given this script doesn't directly provide the functions for a radial tree (and mentions using `treegraph` function not present in standard R libraries), you might explore packages like `ggraph` for similar visualizations or adapt tree representations that reflect hierarchical structures.
Treemap for Advice Network
# You first need to calculate the degrees for the advice network.
advice_deg <- degree(krack_full, mode="all", weights=E(krack_full)$advice_tie)
# Combine this with the advice network connections data
advice_data <- krack_full_nonzero_edges[krack_full_nonzero_edges$advice_tie > 0, c("from", "to", "advice_tie")]
advice_data$degree <- advice_deg[as.character(advice_data$from)]
# Plotting the treemap
treemap(data=advice_data,
index=c('from', 'to'),
vSize='degree',
type="index")
Please, adjust codes as needed based on the actual data structure or any specific requirements of the tasks.