Get Started

Visualizing edges

In this exercise you will learn how to change the size of edges in a network based on their weight, as well as how to remove edges from a network which can sometimes be helpful in more effectively visualizing large and highly clustered networks. In this introductory chapter, we have just scratched the surface of what's possible in visualizing igraph networks. You will continue to develop these skills in future chapters.

This is a part of the course

“Network Analysis in R”

View Course

Exercise instructions

  • Create a vector w1 of edge weights based on the number of hours friends spend together.
  • Plot the network ensuring that the edge.width is set to the vector of weights you just created. Using edge.color = 'black' ensures that all edges will be black.
  • Next, create a new graph object g2 that is the g1 network but with all edges of that are of weight less than two hours removed. This is done by using delete_edges() which takes two arguments. The first is the graph object and the second is the subset of edges to be removed. In this case, you will remove any edges that have a value of less than two hours.
  • Finally, plot the new network g2 using the appropriate vector of edge widths and layout.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

library(igraph)

# Create a vector of weights based on the number of hours each pair spend together
w1 <- E(g1)$___

# Plot the network varying edges by weights
m1 <- layout_nicely(g1)
plot(g1, 
        vertex.label.color = "black", 
        edge.color = 'black',
        edge.width = ___,
        layout = m1)


# Create a new igraph object by deleting edges that are less than 2 hours long 
g2 <- delete_edges(g1, E(g1)[___ < ___])


# Plot the new graph 
w2 <- E(g2)$hours
m2 <- layout_nicely(g2)

plot(g2, 
     vertex.label.color = "black", 
     edge.color = 'black',
     edge.width = ___,
     layout = ___)
Edit and Run Code

This exercise is part of the course

Network Analysis in R

IntermediateSkill Level
4.8+
6 reviews

Learn to analyze and visualize network data with the igraph package and create interactive network plots with threejs.

In this chapter, you will be introduced to fundamental concepts in social network analysis. You will learn how to use the <code>igraph</code> R package to explore and analyze social network data as well as learning how to visualize networks.

Exercise 1: What are social networks?Exercise 2: Creating an igraph objectExercise 3: Counting vertices and edgesExercise 4: Network attributesExercise 5: Node attributes and subsettingExercise 6: Edge attributes and subsettingExercise 7: Visualizing attributesExercise 8: Quiz on attributesExercise 9: Network visualizationExercise 10: igraph network layoutsExercise 11: Visualizing edges
Exercise 12: Quiz on igraph objects

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free