how to increase length of edges when plotting a graph in R - r

Is there a way to plot a graph in r with bigger edge lengths?
I am simply using
library(igraph)
plot(graph)
and do anybody knows why all the edges have variable length?
** V1 V2
1 6 1
2 6 5
3 1 0
4 1 6
5 1 385
6 5 4
7 5 6
8 5 98
9 0 1
10 0 2
I have data in this format and I am generating a network graph.

You could try a few things:
You could change the margins on your plot:
par(mar=c(0,0,0,0))
plot(graph)
You could change the layout parameters by exploring the igraph documentation on layouts to do things like:
test.layout <- layout_(g,with_dh(weight.edge.lengths = edge_density(g)/1000))
plot(g, layout = test.layout)

Related

Change the order of Edges in Network Graph

is there anyway to change the order of the edges in a network graph,
using any of the igraph, visNetwork or even JS within R?
For example i would like a network to have all the arrows going to, from and to;from all in order,
however found nothing online to edit the way the order of the edges is produced,
any help appreciated?
Using igraph you could convert the graph into a data frame and then arrange it:
set.seed(4321)
g <- igraph::sample_gnp(10, .4) %>%
igraph::as.directed()
df <- igraph::as_data_frame(g)
dplyr::arrange(df, from)
This hsould give you something like:
from to
1 1 4
2 1 5
3 1 6
4 1 7
5 1 8
6 1 10
7 2 4
8 2 8
9 2 9
10 2 10

rCharts - interactive matrix plot

I have a horizontally oriented matrix with x = time and y = stocks' returns. I'd like to plot it with rCharts to make it interactive but I can't find HOW anywhere...
matrix is like:
matTest <- as.data.frame(matrix(rnorm(100,0,1), nrow = 5, ncol = 10))
colnames(matTest) <- c('t0','t1','t2','t3','t4','t5','t6','t7','t8','t9')
rownames(matTest) <- c('stock1','stock2','stock3', 'stock4','stock5')
do you know how can I do that?
Thank you very much
If you need an interactive table, you can use this code on your original data.
library(DT)
datatable(matTest, options = list(pageLength = 9))
If you want an interactive time_series plot, first of all change the format of your data in this way:
df<-as.data.frame(cbind(as.matrix(as.vector(t(matTest))),c(1:ncol(matTest)-1),unlist(lapply(rownames(matTest),rep,times=ncol(matTest)))))
colnames(df)<-c("time_series","time","stock")
df
time_series time stock
1 -0.813688587253615 0 1
2 -0.457763419325742 1 1
3 0.0756429812511287 2 1
4 2.18700453503453 3 1
5 1.00659661717065 4 1
6 -2.16436341755656 5 1
7 -0.0829999360152501 6 1
8 -0.491237208736282 7 1
9 0.351591891565934 8 1
10 0.138073915553248 9 1
11 0.276431050047784 0 2
12 -0.88208290628419 1 2
13 0.421498167781597 2 2
...
Now use rCharts to plot yout time_series
library("rCharts")
xPlot(time_series~time, group="stock",data=df,type="line-dotted")
Now you can change the plot's parameters to have the best outfit.

How to plot only large communities/clusters in R

I have an igraph in g. Since the graph is huge I only want to plot communities with more than 10 members, but I want to plot them all in one plot.
My idea to remove unwanted elements is:
g <- delete_vertices(g, V(g)[igraph::clusters(g)$csize < 10])
but for some reason this plots a lot of single nodes, which is the opposite of what I try to achieve. Can you tell me where I am wrong?
Your idea is great, but the problem is that
igraph::clusters(g)$csize < 10
only returns a logical vector of clusters containing fewer than 10 members. Meanwhile, you need to know which vertices belong to those clusters.
Hence, we may proceed as follows.
set.seed(1)
g1 <- erdos.renyi.game(100, 1 / 70)
cls <- clusters(g1)
cls$csize
# [1] 1 1 43 2 11 1 1 1 2 1 2 5 1 1 4 4 1 1 1 1 2 1 2 1
# [25] 4 1 1 1 1 1 # Two clusters of interest
g2 <- delete_vertices(g1, V(g1)[cls$membership %in% which(cls$csize <= 10)])
plot(g2)

Visualize strongly connected components in R

I have a weighted directed graph with three strongly connected components(SCC).
The SCCs are obtained from the igraph::clusters function
library(igraph)
SCC<- clusters(graph, mode="strong")
SCC$membership
[1] 9 2 7 7 8 2 6 2 2 5 2 2 2 2 2 1 2 4 2 2 2 3 2 2 2 2 2 2 2 2
SCC$csize
[1] 1 21 1 1 1 1 2 1 1
SCC$no
[1] 9
I want to visualize the SCCs with circles and a colored background as the graph below, is there any ways to do this in R? Thanks!
Take a look at the mark.groups argument of plot.igraph. Something like the following will do the trick:
# Create some toy data
set.seed(1)
library(igraph)
graph <- erdos.renyi.game(20, 1/20)
# Do the clustering
SCC <- clusters(graph, mode="strong")
# Add colours and use the mark.group argument
V(graph)$color <- rainbow(SCC$no)[SCC$membership]
plot(graph, mark.groups = split(1:vcount(graph), SCC$membership))

How to get member of clusters from R's hclust/heatmap.2

I have the following code that perform hiearchical
clustering and plot them in heatmap.
library(gplots)
set.seed(538)
# generate data
y <- matrix(rnorm(50), 10, 5, dimnames=list(paste("g", 1:10, sep=""), paste("t", 1:5, sep="")))
# the actual data is much larger that the above
# perform hiearchical clustering and plot heatmap
test <- heatmap.2(y)
Which plot this:
What I want to do is to get the cluster member from each hierarchy of in the plot
yielding:
Clust 1: g3-g2-g4
Clust 2: g2-g4
Clust 3: g4-g7
etc
Cluster last: g1-g2-g3-g4-g5-g6-g7-g8-g9-g10
Is there a way to do it?
I did have the answer, after all! #zkurtz identified the problem ... the data I was using were different than the data you were using. I added a set.seed(538) statement to your code to stabilize the data.
Use this code to create a matrix of cluster membership for the dendrogram of the rows using the following code:
cutree(as.hclust(test$rowDendrogram), 1:dim(y)[1])
This will give you:
1 2 3 4 5 6 7 8 9 10
g1 1 1 1 1 1 1 1 1 1 1
g2 1 2 2 2 2 2 2 2 2 2
g3 1 2 2 3 3 3 3 3 3 3
g4 1 2 2 2 2 2 2 2 2 4
g5 1 1 1 1 1 1 1 4 4 5
g6 1 2 3 4 4 4 4 5 5 6
g7 1 2 2 2 2 5 5 6 6 7
g8 1 2 3 4 5 6 6 7 7 8
g9 1 2 3 4 4 4 7 8 8 9
g10 1 2 3 4 5 6 6 7 9 10
This solution requires computing the cluster structure using a different packags:
# Generate data
y = matrix(rnorm(50), 10, 5, dimnames=list(paste("g", 1:10, sep=""), paste("t", 1:5, sep="")))
# The new packags:
library(nnclust)
# Create the links between all pairs of points with
# squared euclidean distance less than threshold
links = nncluster(y, threshold = 2, fill = 1, give.up =1)
# Assign a cluster number to each point
clusters=clusterMember(links, outlier = FALSE)
# Display the points that are "alone" in their own cluster:
nas = which(is.na(clusters))
print(rownames(y)[nas])
clusters = clusters[-nas]
# For each cluster (with at least two points), display the included points
for(i in 1:max(clusters, na.rm = TRUE)) print(rownames(y)[clusters == i])
Obviously you would want to revise this into a function of some kind to be more user friendly. In particular, this gives the clusters at only one level of the dendrogram. To get the clusters at other levels, you would have to play with the threshold parameter.

Resources