#install.packages('igraph')
Load the library ‘igraph’
library(igraph)
##
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
##
## decompose, spectrum
## The following object is masked from 'package:base':
##
## union
Set the working directory
#setwd("graphsInR")
Creating a simple graph
g1 <- graph( edges=c(1,2, 2,3, 3, 1), n=3, directed=F )
plot(g1)
nodes <- read.csv("Dataset1-Media-Example-NODES.csv", header=T, as.is=T)
links <- read.csv("Dataset1-Media-Example-EDGES.csv", header=T, as.is=T)
net <- graph_from_data_frame(d=links, vertices=nodes, direct=T)
class(net)
## [1] "igraph"
#install.packages('networkD3')
#install.packages('visNetwork')
library(networkD3)
library(visNetwork)
Using networkD3, we can bind dataframes to nodes and links to generate interactive, D3.js-style web graphics.
links.d3 <- data.frame(from=as.numeric(factor(links$from))-1,
to=as.numeric(factor(links$to))-1)
nodes.d3 <- cbind(idn=factor(nodes$media, levels=nodes$media),nodes)
Using D3, we can create a force-directed interactive graph. Click on nodes to see effects.
forceNetwork(Links = links.d3, Nodes = nodes.d3, Source="from", Target="to",
NodeID="idn", Group="type.label", linkWidth=1,
linkColour = "#afafaf", fontSize=12, zoom=T,legend=T,
Nodesize=6, opacity = 0.8, charge =-300,
width = 600, height = 400)