以下是從igraph包中獲取的示例:
library(igraph)
actors <- data.frame(name=c("Alice", "Bob", "Cecil", "David", "Esmeralda"))
relations <- data.frame(from=c("Bob", "Cecil", "Cecil", "David","David", "Esmeralda"),
to=c("Alice", "Bob", "Alice", "Alice", "Bob", "Alice"),
friendship=c(4,5,5,2,1,1))
g <- graph_from_data_frame(relations, directed=TRUE, vertices=actors)
該圖如下所示:

但是,我想得到一個不同的情節。目的是提高圖形的質量和美感,例如通過使用ggplot2(或我不知道的其他解決方案)。
該圖不應包含節點之間的鏈接:節點應該彼此靠近,它們之間存在的鏈接的權重越大。節點應該有自己的標簽(如上圖所示),它們的大小應該p與 dataframe變數的值成正比df。
> dput(df)
structure(list(nodes = c("Alice", "Bob", "Cecil", "David", "Esmeralda"
), p = c(4, 3, 2, 2, 1)), class = "data.frame", row.names = c(NA,
-5L))
> df
nodes p
1 Alice 4
2 Bob 3
3 Cecil 2
4 David 2
5 Esmeralda 1
uj5u.com熱心網友回復:
更新
也許你可以使用visNetwork包
library(visNetwork)
nodes <- transform(
setNames(df, c("label", "value")),
id = seq_along(label)
)
edges <- transform(
relations,
from = match(from, nodes$label),
to = match(to, nodes$label),
length = prod(friendship) / friendship
)
visNetwork(nodes, edges) %>%
visEdges(hidden = TRUE)
這使

上一個答案
也許你可以試試下面的代碼
g %>%
set_edge_attr(name = "width", value = E(.)$friendship) %>%
set_vertex_attr(name = "size", value = 10 * with(df, p[match(nodes, names(V(.)))])) %>%
plot()

uj5u.com熱心網友回復:
一種簡單的解決方案是將邊緣值friendship視為邊緣weight。在內部,這些權重依次輸入到igraph( ?layout_nicely;?layout_with_fr見weights引數)的標準布局演算法中:
E(g)$weight <- E(g)$friendship^2 # squaring increases the observed effect
plot(g)
或作為單線:
plot(g, layout = layout_with_fr(g, weights = E(g)$friendship^2)
要省略鏈接,您可以簡單地將邊緣的顏色設定為白色:
plot(g,
layout = layout_with_fr(g, weights = E(g)$friendship^2,
edge.color = 'white'))
或者在更優雅的解決方案中預先計算布局并完全洗掉鏈接。
l <- layout_with_fr(g, weights = E(g)$friendship^2)
g <- delete.edges(g, which(E(g)$friendship > 0))
plot(g, layout = l)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/371958.html
上一篇:測驗彈簧控制器時出現意外回傳碼
