Get startedGet started for free

Coloring nodes

In this exercise, you will color the nodes in the network depending on the churn status. The customers who have churned will be colored red and the non-churners will be colored white. Then you will visualize the network again.

This exercise is part of the course

Predictive Analytics using Networked Data in R

View Course

Exercise instructions

  • Add a node attribute called color that is the same as V(network)$churn.
  • In V(network)$color, use the gsub() function to replace the string "1" with the string "red".
  • In V(network)$color, use the gsub() function to replace the string "0" with the string "white".
  • Visualize the network again using the plot() function.

Hands-on interactive exercise

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

# Add a node attribute called color
V(network)$___ <- V(network)$churn

# Change the color of churners to red and non-churners to white
V(network)$color <- gsub(___, "red", V(network)$color) 
V(network)$color <- gsub(___, "white", V(network)$color)

# Plot the network
___(___, vertex.label = NA, edge.label = NA,
    edge.color = "black", vertex.size = 2)
Edit and Run Code