I have a dataset like following :
link from_node to_node
1 412300044 100923 100185
2 412300047 100190 100198
3 412300057 100197 100198
4 412300058 100198 100199
5 412300076 100190 100199
... ... ... ...
I will use 'link' variable as an edge, and 'from_node' and 'to_node' as vertices. No weighted value currently. I decided this, but have no idea what can I do further. I searched internet about making edge betweenness figure but there were very few...I would be very helpful if you help me.
CodePudding user response:
We don't have your data set, but here's one that approximates it:
set.seed(1234)
df <- data.frame(link = sample(30, 10) 412300040,
from_node = sample(10, 10, T) 100184,
to_node = sample(10, 10, T) 100184)
df
#> link from_node to_node
#> 1 412300068 100188 100189
#> 2 412300056 100186 100192
#> 3 412300066 100191 100188
#> 4 412300062 100190 100192
#> 5 412300045 100194 100187
#> 6 412300052 100190 100188
#> 7 412300055 100188 100194
#> 8 412300049 100192 100189
#> 9 412300070 100188 100186
#> 10 412300046 100188 100192
If you simply want to create a graph and calculate edge betweenness using igraph, you can do:
library(igraph)
ig <- graph_from_data_frame(df[c(3, 2, 1)])
edge_betweenness(ig)
#> [1] 2.5 2.0 6.0 1.5 4.0 4.5 6.0 2.5 3.0 2.0
To plot with edges weighted according to betweenness, you could do:
plot(ig, edge.width = edge_betweenness(ig), edge.label = df$link)
However, I think if you want to plot graphs, you should check out the tidygraph and ggraph packages, which allow easy manipulation of graphs, and highly customizable plots of graphs respectively. Here's how you could plot your graph with edges weighted according to betweenness:
library(tidygraph)
library(ggraph)
as_tbl_graph(df[c(3, 2, 1)]) %>%
activate(edges) %>%
mutate(betweenness = centrality_edge_betweenness()) %>%
ggraph()
geom_edge_diagonal(aes(edge_width = betweenness, label = link),
angle_calc = "along", alpha = 0.5, vjust = -1,
color = "lightblue")
geom_node_circle(aes(r = 0.2), fill = "lightblue", color = "navy")
geom_node_text(aes(label = name))
coord_equal()
theme_graph()
CodePudding user response:
Try this.
require(igraph)
df <- data.frame(
edge = c(412300044, 412300047, 412300057, 412300058, 412300076),
from_node = c(100923, 100190 , 100197 , 100198 , 100190),
to_node = c(100185, 100198 , 100198 , 100199 , 100199)
)
# reorder, rename columns
df2 <- data.frame(to_node=df[,2], from_node=df[,3], label=df[,1])
g <- graph_from_data_frame(df2, directed=TRUE)
plot(g)
Note that the command help(graph_from_data_frame) can help you further.