Change the order of Edges in Network Graph - r

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

Related

how to increase length of edges when plotting a graph in 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)

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))

Finding the set of n equally distant points [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
For simplicity, if I have a vector of points which looks something like:
x = c(1,4,5,8,9)
I'm trying to find the n points which are equidistant from one another. In this case my n=3 so my ideal answer would be:
1,5,9
Since 5-1=4 and 9-5=4.
The actual vectors are much larger/complex as well as n.
Any ideas on how I can achieve this?
Thanks in advance!
This isn't the whole solution, but I think it is the start of one. First, computing the distance matrix will probably be helpful.
> x <- c(1,4,5,8,9)
> dx <- dist(x)
> dx
1 2 3 4
2 3
3 4 1
4 7 4 3
5 8 5 4 1
Second, you can identify points which are the same distance apart by sorting the distances and run-length encoding them.
> rdx <- rle(sort(dx))
> rdx
Run Length Encoding
lengths: int [1:6] 2 2 3 1 1 1
values : num [1:6] 1 3 4 5 7 8
you can select the set of points you want and then get back to the indices in the original distance matrix using the order function. Taking the third group -- of points separated by distance 4 -- as an example
> i=3
> orderedIndex <- sum(rdx$lengths[1:(i-1)])
> order(dx)[(orderedIndex+1):(orderedIndex+rdx$lengths[i])]
[1] 2 6 9
(the indices count from the top down then from left to right). So here you have identified the 4s in the distance matrix: these are distances between the 1st/3rd, 2nd/4th, and 3rd/5th points in x. But you still have to do some more work to eliminate the 2nd and 4th points. Presumably you choose the 1st, 3rd and 5th points because they are connected?
I think you would want to process all groups of points identified by the rle function as over your chosen size, and then check for connectivity.
Consistent with comments above, here's something that might be what you want, not necessarily what you ask for. I'm sure there is a more efficient way to do this, though.
x = c(1,4,5,8,9)
x2 <- as.matrix(expand.grid(x, x))
x2 <- as.data.frame(t(apply(x2, 1, sort)))
x2 <- x2[!duplicated(x2), ]
x2 <- cbind(x2, d =abs(mapply("-", x2[,1], x2[,2])))
x2[order(x2$d), ]
# V1 V2 d
# 1 1 1 0
# 7 4 4 0
# 13 5 5 0
# 19 8 8 0
# 25 9 9 0
# 8 4 5 1
# 20 8 9 1
# 2 1 4 3
# 14 5 8 3
# 3 1 5 4
# 9 4 8 4
# 15 5 9 4
# 10 4 9 5
# 4 1 8 7
# 5 1 9 8

Creating a histogram with the number of occurrences at multiple places

I have a set of data that looks like that (just way bigger):
2 7
3 9
5 3
2 4
7 3
3 4
2 2
and I would like to produce a histogram with bars at 2 of height (7+4+2), so 13, at 3 of height 13, 5 at 3 and 7 at 3.
I hope the question is not too dumb, but the tutorials I found did not discuss this problem. Thanks for any help in advance.
DF <- read.table(text="2 7
3 9
5 3
2 4
7 3
3 4
2 2")
library(ggplot2)
ggplot(DF,aes(x=V1,y=V2)) +stat_summary(fun.y=sum,geom="bar")
If you want to get the aggregated sums out of the data and plot them later (the ggplot solution does it all) then, starting from DF:
> aggregate(V2~V1,data=DF,sum)
V1 V2
1 2 13
2 3 13
3 5 3
4 7 3
Other answers given here probably already answer your question, but for the sake of completeness, if you do not wish to depend on the ggplot package (I cannot really think of a reason for this, but you might) you could use a combination of aggregate and barplot.
> ADF <- aggregate(DF$V2, by = list(V1=DF$V1), FUN = sum)
> barplot(ADF$x, names.arg=ADF$V1)

Plot only (x,y) points with a specified z value

I have x,y,z data in 3 columns like this:
1 2 1
2 4 1
3 3 1
4 4 2
5 8 2
6 6 2
Say I only wanted to plot just (x,y) values where z=2 (i.e, just last 3 rows). How do I do that within gnuplot?
plot 'datafile.dat' using 1:((column(3) == 2) ? column(2):NaN)
Note that you can also use the shorthand form: $3 instead of column(3). I just used the latter form because it is easier to read.

Resources