Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require(igraph)
- require(dplyr)
- # Simulate some smaller scale data:
- set.seed(123)
- # fake IP addresses, assume counts follow normal dist (some heavy browsers, some light browsers)
- IPcounts <- round(rnorm(100,mean=10,sd=3))
- # Repeat IPs by fake counts
- IP <- NULL
- for(i in 1:length(IPcounts)){
- IP <- append(IP,rep(i,IPcounts[i]))
- }
- # fake domains, assume inverse popularity of visits (some very popular domains, many less popular domains)
- domainCounts <- round(1000*(1/c(1:10000))+1)
- # repeat domains by popularity
- domain <- NULL
- for(i in 1:length(domainCounts)){
- domain <- append(domain,rep(i,domainCounts[i]))
- }
- # randomize the domain list
- domain <- sample(domain)
- # randomly sample IPs to match the domain list's size
- IP <- sample(IP,length(domain),replace=T)
- # form the data frame
- testData <- data.frame(domain,IP)
- # transpose the incidence matrix to form a one-mode adjacency matrix
- testData <- tcrossprod(table(testData))
- # Use igraph to form up a graph of the data above
- testNet <- graph.data.frame(testData, directed = F,)
- # See nodes
- V(testNet)
- # See edges
- E(testNet)
- # inspect degrees
- degree(testNet)
- # Change node size to vary with degree
- V(testNet)$size<-0.5
- V(testNet)$color<-'white'
- # Plot the result
- par(mai=c(0,0,1,0))
- par(bg = 'black')
- set.seed(123)
- plot(testNet,
- layout=layout.fruchterman.reingold,
- main='SmallWeb',
- vertex.frame.color='white',
- vertex.label = NA,
- # edge.curved=F,
- edge.arrow.size = 0.2, # for directed plot
- edge.color = hsv(0.5,0.9,1,alpha=0.3)
- )
- # Remove low degree connections
- bad.vs<-V(testNet)[degree(testNet)<2] #identify those vertices part of less than 2 edges
- testNet<-delete.vertices(testNet, bad.vs) #exclude them from the graph
- set.seed(123)
- plot(testNet,
- layout=layout.fruchterman.reingold,
- main='SmallWeb',
- vertex.frame.color='white',
- vertex.label = NA,
- # edge.curved=F,
- edge.arrow.size = 0.2, # for directed plot
- edge.color = hsv(0.5,0.9,1,alpha=0.3)
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement