R: "Connecting" Graphs

R: connecting graphs

For the binary case, where the question is whether a connection between communities exists or not, you could do the following. First, contract the communities based on their membership (belonging to "a", "b", or "c").

V(karate)$mem <- membership(cfg)
g <- contract.vertices(karate, V(karate)$mem, vertex.attr.comb = "ignore")

Secondly, remove loops and multiple edges:

g <- simplify(g)

Finally, extract adjacent nodes (which now equal communities after contracting them above):

sapply(1:vcount(g), function(x) adjacent_vertices(g, x)) 

Connecting All Nodes Together on a Graph

You could just update relations using complete, and than filter out the rows where from is equal to to, which gives arrows from a node to itself.

relations <- relations %>% 
complete(from, to) %>%
dplyr::filter(from != to)

Connecting points in ggplot2 graph with lines

From @Duck's comment: just add + geom_line() or + geom_path() to a plot. ("Which to use" depends on your use: geom_line connects by the order on the x-axis, geom_path connects in the order they appear in the data, both have valid uses. In this case they appear to be the same, but keep this in mind if your real data changes.)

ggplot(popdata, aes(predator, prey, color = k)) +
geom_point() +
geom_line()

ggplot2 with connected lines

(Similarly for the other plots.)

I should note that order matters.

# left
ggplot(popdata, aes(predator, prey, color = k)) +
geom_point() +
geom_line(color="black")
# right
ggplot(popdata, aes(predator, prey, color = k)) +
geom_line(color="black") +
geom_point()

ggplot2, side-by-side, point-over-line and reverse

how to connect directional sub-graphs in R igraph

You can try the code below if you have more than one gap (i.e., more than two clusters)

e <- c(sapply(decompose(g),function(x) names(V(x))[degree(x)==1]))
G <- g %>%
add.edges(e[2:(length(e)-1)],weight = vcount(g))

such that

> get.data.frame(G)
from to weight attr
1 1 2 1 0.1
2 2 3 1 0.1
3 4 5 1 0.1
4 5 6 1 0.1
5 7 8 1 0.1
6 8 9 1 0.1
7 3 4 9 NA
8 6 7 9 NA

Data

df <-
data.frame(
from = c(1, 2, 4, 5, 7, 8),
to = c(2, 3, 5, 6, 8, 9),
weight = c(1, 1, 1, 1, 1, 1),
attr = c(0.1, 0.1, 0.1, 0.1, 0.1, 0.1)
)

How to connect components of a graph in igraph

Try the code below

graph2 <- with(
stack(membership(cl)),
add.edges(
graph1,
c(combn(sapply(split(ind, values), sample, size = 1), 2)),
weight = runif(choose(cl$no, 2))
)
)

where a full connections among all components is given as below
Sample Image


You can check the weights of edges of graph2

> E(graph2)
+ 27/27 edges from 31a7614 (vertex names):
[1] ID_00104--ID_06663 ID_00104--ID_06791 ID_00104--ID_09099 ID_00136--ID_00169
[5] ID_00136--ID_00910 ID_00169--ID_00910 ID_00178--ID_00790 ID_00180--ID_01013
[9] ID_00180--ID_01130 ID_00180--ID_01260 ID_00180--ID_00394 ID_00180--ID_00860
[13] ID_00180--ID_00959 ID_00180--ID_01222 ID_00180--ID_00288 ID_00180--ID_00324
[17] ID_00180--ID_00663 ID_00180--ID_00846 ID_00180--ID_01047 ID_00180--ID_06781
[21] ID_00180--ID_06786 ID_00136--ID_06663 ID_00178--ID_06663 ID_06663--ID_01013
[25] ID_00136--ID_00178 ID_00136--ID_01013 ID_00178--ID_01013

> E(graph2)$weight
[1] 0.6541726 0.9190969 0.9258216 0.8604061 0.7463768 0.7671958 0.8303797
[8] 0.6615776 0.7075209 0.9081935 0.6571188 0.6876640 0.6858639 0.8745136
[15] 0.8366465 0.6183618 0.6841637 0.9147287 0.8762976 0.7327071 0.7731164
[22] 0.3098355 0.5842953 0.7397926 0.7337629 0.7643245 0.8331742

If you want to see edge list, you can use as_data_frame, e.g.,

> as_data_frame(graph2)
from to new_ssp weight
1 ID_00104 ID_06663 0.6541726 0.6541726
2 ID_00104 ID_06791 0.9190969 0.9190969
3 ID_00104 ID_09099 0.9258216 0.9258216
4 ID_00136 ID_00169 0.8604061 0.8604061
5 ID_00136 ID_00910 0.7463768 0.7463768
6 ID_00169 ID_00910 0.7671958 0.7671958
7 ID_00178 ID_00790 0.8303797 0.8303797
8 ID_00180 ID_01013 0.6615776 0.6615776
9 ID_00180 ID_01130 0.7075209 0.7075209
10 ID_00180 ID_01260 0.9081935 0.9081935
11 ID_00180 ID_00394 0.6571188 0.6571188
12 ID_00180 ID_00860 0.6876640 0.6876640
13 ID_00180 ID_00959 0.6858639 0.6858639
14 ID_00180 ID_01222 0.8745136 0.8745136
15 ID_00180 ID_00288 0.8366465 0.8366465
16 ID_00180 ID_00324 0.6183618 0.6183618
17 ID_00180 ID_00663 0.6841637 0.6841637
18 ID_00180 ID_00846 0.9147287 0.9147287
19 ID_00180 ID_01047 0.8762976 0.8762976
20 ID_00180 ID_06781 0.7327071 0.7327071
21 ID_00180 ID_06786 0.7731164 0.7731164
22 ID_06791 ID_00910 NA 0.5680686
23 ID_00178 ID_06791 NA 0.6278790
24 ID_06791 ID_00288 NA 0.8059711
25 ID_00178 ID_00910 NA 0.2578959
26 ID_00910 ID_00288 NA 0.6189977
27 ID_00178 ID_00288 NA 0.6594792

Attaching Multiple Graphs Together in R

We may try in gifski animation in a markdown page

---
title: "Title"
output: html_document

---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown

```{r data, echo = FALSE}
suppressPackageStartupMessages(library(gifski))
a = abs(rnorm(100,10,10))
b = abs(rnorm(100,10,10))
i = 1
c = data.frame(a,b,i)
c$col = as.factor(ifelse(c$b>10,"red", "blue"))

a = abs(rnorm(100,10,10))
b = abs(rnorm(100,10,10))
i = 2
d = data.frame(a,b,i)
d$col = as.factor(ifelse(d$a>10,"red", "blue"))

a = abs(rnorm(100,10,10))
b = abs(rnorm(100,10,10))
i = 3
e = data.frame(a,b,i)
e$col = as.factor(ifelse(e$b>10,"red", "blue"))

#put all the datasets together (i=1, i=2, i=3)
g = rbind(c,d,e)

lst1 <- list(c, d, e)

```

```{r, animation.hook="gifski"}
for (i in seq_along(lst1)) {
plot(lst1[[i]]$a, type = 'h', col = lst1[[i]]$col,
main = paste0("plot_", i))
}
```

Or if we just want to save the file as gif

library(gifski)
lst1 <- list(c, d, e)
gif_file <- "/path/to/file.gif")
save_gif(
for (i in seq_along(lst1)) {
plot(lst1[[i]]$a, type = 'h', col = lst1[[i]]$col, main = paste0("plot_", i))
}, gif_file, 1280, 720, res = 144)

Are a specified set of nodes in a graph connected?

You are just asking if the induced subgraph is connected, so compute the subgraph and test if it is connected.

my.function = function(g, nodes) {
is_connected(subgraph(g, nodes)) }

my.function(my.graph, c("b", "a", "c"))
[1] TRUE
my.function(my.graph, c("b", "a", "e"))
[1] TRUE
my.function(my.graph, c("b", "a", "f"))
[1] FALSE

R: connect points on a graph (ggplot2)

Here's a solution using the tidyverse package. I used the number of each row in the original data as the y-axis values in the plot. As these values are meaningless, I removed the y-axis title, labels and ticks from the plot.

library(tidyverse)

Data %>%
# Number each row in its order of appearance,
# save this numbers in a new column named order
rowid_to_column("order") %>%
# Change data from wide to long format
pivot_longer(cols = c(start_date, end_date),
names_to = "date_type",
values_to = "date") %>%
# Ggplot, use date as x, order as y, ID as col and order as group
ggplot(aes(x = date,
y = order,
col = ID,
group = order)) +
# Draw points
geom_point()+
# Draw lines
geom_line() +
# Maybe you want to remove the y axis title, text and ticks
theme(axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
# I added a vertical format to the x axis labels
# it might easier to read this way
axis.text.x = element_text(angle = 90, vjust = 0.5))

points-linesplot

R: Extract connected graphs

You can do it like this:

library(igraph)
set.seed(1)
g <- simplify(graph.compose(graph.ring(10), graph.star(5, mode = "undirected"))) + edge("7", "8")
dg <- decompose.graph(g)
t(sapply(seq_along(dg), function(x) c(Subgraph = x, NodeCount = vcount(dg[[x]]), EdgeCount = ecount(dg[[x]]))))
# Subgraph NodeCount EdgeCount
# [1,] 1 7 12
# [2,] 2 2 1
# [3,] 3 1 0

The example graph looks like this (plot(g)):

Sample Image



Related Topics



Leave a reply



Submit