Why social distancing is important to fight Corona virus — A Network analysis of Corona Virus in R

Harish Nagpal
Analytics Vidhya
Published in
7 min readMar 29, 2020
Corona chain — 1 to 1000

Corona virus has become a nightmare for human race all over the world. The virus which started its journey from Wuhan, China has spread almost in all countries.

Never in the history of its mankind, human faced so much threat to their life, the reason being there doesn’t seem to be any medicine or vaccination so far which acts as resistance to the virus the moment it enters the human body.

I have tried to do a small visualization in R with networkD3 package.

You can read more about networkD3 package here. As per cran website, this package creates ‘D3’ ‘JavaScript’ network, tree, dendrogram, and Sankey graphs from ‘R’. You can create different types of network diagrams with this package.

You can create simple java script force directed network graphs with simpleNetwork function. You have to show the source and target nodes and it does the job for you. You will get a moving network diagram with source and destination. You can also drag nodes with mouse.

But simpleNetwork does not show the nodes in different colors if you want to. Since corona spreads from one person to another person so I wanted to show something similar. So I decided to use forceNetwork function. With this function you can show the nodes in different colors, show source and target nodes, you can group them and so on.

Here is the full forceNetwork function as mentioned at cran website. You can read full details as given in the link earlier.

forceNetwork(Links, Nodes, Source, Target, Value, NodeID, Nodesize, Group, height = NULL, width = NULL, 
colourScale = JS(“d3.scaleOrdinal(d3.schemeCategory20);”),
fontSize = 7, fontFamily = “serif”, linkDistance = 50,
linkWidth = JS(“function(d) { return Math.sqrt(d.value); }”), radiusCalculation = JS(“ Math.sqrt(d.nodesize)+6”), charge = -30, linkColour = “#666”, opacity = 0.6, zoom = FALSE, legend = FALSE, arrows = FALSE, bounded = FALSE, opacityNoHover = 0, clickAction = NULL)

forceNetwork function requires two data sets. One data set with source node, target node and width of the line connecting two nodes and another one with name, id and group. The id in the 2nd data set is same as source node or target node from 1st data set. The group in the 2nd data set denotes to which group that id belongs to. The group argument helps in coloring the nodes.

The data sets given in the example at cran website uses Les Miserables movie data sets. You must have heard the song I dreamed a dream by Susan Boyle which made her an overnight singing sensation after she sang this song in Britain’s Got Talent. The song was from the same movie. I find this song very motivating.

I created my own data set by creating two data sets as nodes and edges. My intention was to show how a corona infected person infects two more persons with corona virus, who in turn infects four more and so on and the chain continues. I used around 1000 data points in my data set and divided them into 500 groups.

So lets start our coding in R.

Make sure that you download the package networkD3.

Lets call the library networkD3, set our working directory and read two files.

library(networkD3)setwd(“D:/Harish/R practice projects/Corona”)edges <- read.csv(“edges22.csv”, header = T)
nodes <- read.csv(“nodes22.csv”, header = T)

See whats inside the data sets.

Edges data set
Nodes data set

Let us run the code with 4 persons named A, B, C and D.

edges1 <- head(edges, n = 2)
nodes1 <- head(nodes, n = 4)
forceNetwork(Links = edges1, Nodes = nodes1,
Source = “source”,
Target = “target”,
fontSize = 16,
NodeID =”name”,
Group = “group”, Value = “width”, opacity = 5.0, zoom = TRUE,
arrows = TRUE,
colourScale = JS(“d3.scaleOrdinal(d3.schemeCategory10);”))
Plot 1

In above plot (Plot 1), A (in blue) has infected B (in orange) and C(in orange). D (in green) is roaming around. Let’s see whether D maintains social distancing or not.

I changed, n to 3 in edges1 and see now D is also infected (see Plot 2) after running below code.

edges1 <- head(edges, n = 3)
nodes1 <- head(nodes, n = 4)
forceNetwork(Links = edges1, Nodes = nodes1,
Source = “source”,
Target = “target”,
fontSize = 16,
NodeID =”name”,
Group = “group”, Value = “width”, opacity = 5.0, zoom = TRUE,
arrows = TRUE,
colourScale = JS(“d3.scaleOrdinal(d3.schemeCategory10);”))
Plot 2

Let’s further increase n values in edges1 and nodes1 data sets to 5 and 10 respectively. And see what happens when we run below code.

edges1 <- head(edges, n = 5)
nodes1 <- head(nodes, n = 10)
forceNetwork(Links = edges1, Nodes = nodes1,
Source = “source”,
Target = “target”,
fontSize = 16,
NodeID =”name”,
Group = “group”, Value = “width”, opacity = 5.0, zoom = TRUE,
arrows = TRUE,
colourScale = JS(“d3.scaleOrdinal(d3.schemeCategory10);”))

A (in blue) has infected B (in orange) and C (in orange). B has further infected E(in green) and D (in green). C (in orange) has further infected F (in red). H (in red) , I (in red), J (in purple) and G (in green) are roaming around.

Plot 3

What if H, I, J and G are also careless and roam around freely. I further increased the n counts in below code. See (in Plot 4 below) now H (in red) , I (in red), J (in purple) and G (in green) also got infected.

edges1 <- head(edges, n = 10)
nodes1 <- head(nodes, n = 15)
forceNetwork(Links = edges1, Nodes = nodes1,
Source = “source”,
Target = “target”,
fontSize = 16,
NodeID =”name”,
Group = “group”, Value = “width”, opacity = 5.0, zoom = TRUE,
arrows = TRUE,
colourScale = JS(“d3.scaleOrdinal(d3.schemeCategory10);”))
Plot 4

I increased the counts to 40 and 100 respectively. See now what happens in below plot (plot 5).

edges1 <- head(edges, n = 40)
nodes1 <- head(nodes, n = 100)
forceNetwork(Links = edges1, Nodes = nodes1,
Source = “source”,
Target = “target”,
fontSize = 16,
NodeID =”name”,
Group = “group”, Value = “width”, opacity = 5.0, zoom = TRUE,
arrows = TRUE,
colourScale = JS(“d3.scaleOrdinal(d3.schemeCategory10);”))
Plot 5

I increased the count to 500, see how the chain explodes (Plot 6).

Plot 6

I increased count to 700 plus and see how it explodes further (Plot 7). People are not maintaining social distancing and paying price for it.

Plot 7

I put all the data points from both data sets and see the full explosion in Plot 8 below with 1000 data points.

library(networkD3)setwd(“D:/Harish/R practice projects/Corona”)edges <- read.csv(“edges22.csv”, header = T)
nodes <- read.csv(“nodes22.csv”, header = T)
head(edges, n = 10)head(nodes, n = 10)#edges1 <- head(edges, n = 40)
#nodes1 <- head(nodes, n = 100)
forceNetwork(Links = edges, Nodes = nodes,
Source = “source”,
Target = “target”,
fontSize = 16,
NodeID =”name”,
Group = “group”, Value = “width”, opacity = 5.0, zoom = TRUE,
arrows = TRUE,
colourScale = JS(“d3.scaleOrdinal(d3.schemeCategory10);”))

Here is the final plot with full 1000 data points.

Plot 8

So many people paid the price of not maintain social distancing.

Hope you liked my article. I have given a basic idea of network analysis with R with limited data points.You can improve the code further with more ideas and data points.

Do give a clap, if you like it.

Have a look at my other articles on Medium.com

https://medium.com/analytics-vidhya/flight-of-corona-a-visualization-journey-around-the-earth-in-r-fe7d82a41b1?source=friends_link&sk=1a954c9730fd30331c71c95e0e7bc9fc

Data science use cases in Life Insurance

See my data science maths art here

Connect with me on Linkedln here.

--

--

Harish Nagpal
Analytics Vidhya

An IT professional, passionate about art in all forms — data, nature, paintings and visual art. Linkedln — https://www.linkedin.com/in/harish-nagpal-8696529/