提问人:Ream 提问时间:10/25/2023 更新时间:10/31/2023 访问量:117
我有节点、边和权重。如何从 R shiny 中的节点、边、权重信息绘制网络
I have nodes,edges, and weights. How can i draw the network from nodes,edges,weight info in R shiny
问:
我制作了以下网络:
p <- 100 # Number of nodes (genes) in the network.
n <- 100 # Number of samples to generate.
network <- random_network(p) # Create a random network of size p.
network <- gen_partial_correlations(network) # Generate weights.
df_ref <- SeqNet::reference$rnaseq # The reference dataset
g <-network
现在从 g 中,我可以同样将节点和边提取为列表:
nodeslist<- list(g$modules[[1]]$nodes)
edgeslist<- list(g$modules[[1]]$edges)
注意:nodeslist 和 edgeslist 包含以下信息: 边缘列表:
weight
[1,] 1 49 0.13920793
[2,] 2 19 -0.16105155
[3,] 2 29 -0.11896011
[4,] 2 49 0.10574541
[5,] 3 29 -0.13120939
[6,] 3 49 0.17390062
[7,] 4 10 -0.10381197
[8,] 5 33 -0.13458892
[9,] 6 49 0.17523747
[10,] 7 19 0.18709664
[11,] 8 49 -0.10200550
[12,] 9 10 -0.16075495
[13,] 9 49 0.12597721
[14,] 10 11 0.12419045
[15,] 10 18 0.16554176
[16,] 10 23 0.12257816
[17,] 10 24 0.17552470
[18,] 10 25 -0.18485569
[19,] 10 28 -0.18851376
[20,] 10 29 -0.17173121
[21,] 10 32 0.14237557
[22,] 10 33 -0.15823895
[23,] 10 34 0.12432978
[24,] 10 35 -0.16700940
[25,] 10 37 0.17640358
[26,] 10 43 -0.10804470
[27,] 10 44 0.16079198
[28,] 10 45 -0.13074089
[29,] 10 47 -0.14941716
[30,] 10 48 0.17069436
[31,] 10 49 -0.14465740
[32,] 10 50 -0.16713456
[33,] 10 51 -0.12124140
[34,] 10 53 0.09832430
[35,] 10 55 -0.16044640
[36,] 10 62 0.15869645
[37,] 10 63 0.11175158
[38,] 12 49 -0.19139133
[39,] 13 29 0.14681721
[40,] 13 49 0.13493487
[41,] 14 33 -0.13364581
[42,] 15 19 0.10663959
[43,] 15 49 -0.11776805
[44,] 16 49 -0.17496817
[45,] 17 49 0.13980787
[46,] 19 24 0.16020803
[47,] 19 25 -0.12007548
[48,] 19 26 0.13061010
[49,] 19 30 0.18347088
[50,] 19 32 0.17066294
[51,] 19 33 -0.16345597
[52,] 19 37 0.12446608
[53,] 19 46 0.13531279
[54,] 19 49 0.10353267
[55,] 19 52 -0.12596833
[56,] 19 63 0.11966468
[57,] 20 49 -0.13018907
[58,] 21 33 -0.10584546
[59,] 21 49 -0.16366432
[60,] 22 29 -0.12769928
[61,] 24 29 0.09683651
[62,] 24 49 -0.17398705
[63,] 25 30 -0.18163093
[64,] 25 34 0.13093457
[65,] 25 35 0.18703481
[66,] 25 41 0.11050505
[67,] 25 44 -0.09962966
[68,] 25 47 0.18286832
[69,] 26 29 0.14178880
[70,] 26 33 -0.10631235
[71,] 27 37 -0.14746540
[72,] 28 33 0.09948649
[73,] 29 30 0.13360557
[74,] 29 31 -0.16245800
[75,] 29 32 -0.16785796
[76,] 29 33 -0.15289176
[77,] 29 39 -0.17283268
[78,] 29 43 0.10077533
[79,] 29 49 -0.13001863
[80,] 29 50 -0.11811612
[81,] 29 51 -0.17453024
[82,] 29 53 -0.12620716
[83,] 29 59 -0.16878030
[84,] 29 63 0.11351594
[85,] 30 37 0.15382473
[86,] 30 49 0.18885891
[87,] 31 49 0.16179096
[88,] 32 49 0.15855171
[89,] 33 34 -0.10088338
[90,] 33 41 0.12001003
[91,] 33 43 -0.18284373
[92,] 33 46 -0.16394107
[93,] 33 49 0.15275365
[94,] 33 53 -0.14424536
[95,] 33 62 -0.18298613
[96,] 35 49 0.15671584
[97,] 36 49 -0.19092302
[98,] 37 49 0.12580630
[99,] 37 59 -0.10462770
[100,] 38 49 -0.12257773
[101,] 40 49 -0.09667345
[102,] 41 49 0.10453115
[103,] 42 49 -0.11113257
[104,] 43 49 -0.10493318
[105,] 48 49 0.18244989
[106,] 49 51 0.15013766
[107,] 49 52 -0.15366076
[108,] 49 54 0.11142246
[109,] 49 56 -0.13114785
[110,] 49 57 -0.17502184
[111,] 49 58 0.18643826
[112,] 49 59 -0.17610071
[113,] 49 60 0.11064330
[114,] 49 61 -0.13522543
nodes列表:
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13
我拥有绘制此网络所需的信息,我该怎么做?如果我尝试这样的事情:
plot(g)
它只在仪表板上绘制为图像,我怎样才能画出来使其具有交互性?
答:
您可以使用将绘图转换为交互式绘图。plotly
plotly::plotly_build
:描述:
此泛型函数创建发送到 plotly.js 进行渲染的列表对象。使用此函数可用于覆盖 ggplotly/plot_ly 提供的默认值或调试渲染错误。
Plotly
R 开源图形库Plotly 的 R 图形库可制作交互式、出版质量的图形。
plot_object<- plot(g)
plotly_object<-plotly::ggplotly(plot_object)
p<-plotly::plotly_build(plotly_objet)
p$elementId <- NULL
# display the interactive plot:
p
根据版本的不同,尝试不这样做:p$elementId <- NULL
plotly
编辑:
igraph
似乎不适用于 Ploty
:
评论
dput(mydata)
plot_object<- g
plotly
这是另一个受 R-bloggers 启发的答案:R 中的网络图,因为在我阅读的评论之后,不支持 .igraph
plotly
请注意,我没有验证网络的逻辑。
您需要运行第一部分和第二部分才能运行有关 networkD3 的第三部分。
有些权重是负面的:我不确定这对网络库来说是件好事,也不确定它们是否为此实现了良好的效果。
flat_dataset <-"r a b weight
[1,] 1 49 0.13920793
[2,] 2 19 -0.16105155
[3,] 2 29 -0.11896011
[4,] 2 49 0.10574541
[5,] 3 29 -0.13120939
[6,] 3 49 0.17390062
[7,] 4 10 -0.10381197
snip snip snip
[113,] 49 60 0.11064330
[114,] 49 61 -0.13522543
"
data <-read.table(textConnection(flat_dataset),header=TRUE)
您必须通过完成更改数据:上面的行只是为了有数据来测试它。
print(as_tibble(data))
# A tibble: 114 × 4
r a b weight
<chr> <int> <int> <dbl>
1 [1,] 1 49 0.139
2 [2,] 2 19 -0.161
3 [3,] 2 29 -0.119
4 [4,] 2 49 0.106
5 [5,] 3 29 -0.131
6 [6,] 3 49 0.174
7 [7,] 4 10 -0.104
8 [8,] 5 33 -0.135
9 [9,] 6 49 0.175
10 [10,] 7 19 0.187
# … with 104 more rows
# ℹ Use `print(n = ...)` to see more rows
以下已加载的包裹。它们必须先安装。
library(tidyverse)
library(igraph))
library(ggraph)
library(networkD3)
library(tidygraph)
第 1 部分 igraph
文章的一部分,但包含您的数据。数据似乎不完整:几乎所有节点都是孤立的。 如果没有分位数滤波器,它们看起来并不完整。igraph
graph <- igraph::graph_from_data_frame(data, directed = FALSE)
plot(graph)
# Warning message in v(graph):
# “Non-positive edge weight found, ignoring all weights during graph layout.”
data2 <- data[data$weight >= stats::quantile(data$weight, p = 0.98),,]
data2
如果您想要所有数据,则必须在下面代替上述操作:
data2 <- data
第1个图形,但带有分位数滤波器:
data2 %>%
igraph::graph_from_data_frame(directed = FALSE) %>%
plot(layout = layout_with_graphopt)
没有分位数滤波器的第一个图形:带有:data2 <- data
第 2 部分和tidygraph
ggraph
nodes <- c(data$a, data$b) %>%
unique() %>%
# tibble from tidyverse library
tibble::tibble(label = .) %>%
tibble::rowid_to_column("id")
# nodes
与标签和 ID 中的重量相同:
nodes <- c(data$a, data$b, data$weight) %>%
unique() %>%
tibble::tibble(label = .) %>%
tibble::rowid_to_column("id")
我想(也许很糟糕)对于,那就是:tidygraph::tbl_graph()
a
是from
b
是to
weight
是N
edges <- data2 %>%
# join the example, but your network seems already made
# dplyr::left_join(nodes, by = c("Person1"="label")) %>%
# dplyr from tidyverse package
dplyr::rename(N = "weight") %>%
dplyr::rename(from = "a") %>%
# join the example, but your network seems already made
# dplyr::left_join(nodes, by = c("Person2"="label")) %>%
dplyr::rename("to" = "b") %>%
dplyr::select(from, to, N)
graph_tidy <- tidygraph::tbl_graph(nodes = nodes, edges = edges, directed = FALSE)
graph_tidy
这给了我:
# A tbl_graph: 63 nodes and 3 edges
#
# An unrooted forest with 60 trees
#
# A tibble: 63 × 2
id label
<int> <int>
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
# … with 57 more rows
#
# A tibble: 3 × 3
from to N
<int> <int> <dbl>
1 7 19 0.187
2 25 35 0.187
3 30 49 0.189
graph_tidy %>%
dplyr::mutate(Centrality = tidygraph::centrality_authority()) %>%
ggraph::ggraph(layout = "graphopt") +
ggraph::geom_node_point(aes(size=Centrality, colour = label), show.legend = FALSE) +
ggraph::geom_edge_link(aes(width = N), alpha = 0.8, show.legend = FALSE) +
ggraph::scale_edge_width(range = c(0.2, 2)) +
ggraph::geom_node_text(aes(label = label), repel = TRUE)
# Warning message:
# “Using the `size` aesthetic in this geom was deprecated in ggplot2 3.4.0.
# Please use `linewidth` in the `default_aes` field and elsewhere instead.”
# my comment: linewidth doesn't work there
没有分位数滤波器的相同图形:with(在开头):data2 <- data
将权重作为 id 和标签的一部分:
第 3 部分networkD3
edges <- edges %>%
dplyr::mutate(from = from -1, to = to - 1) %>%
dplyr::mutate(N = N / 200)
# edges
nodes <- nodes %>%
dplyr::mutate(id=id-1) %>%
dplyr::mutate(nodesize = igraph::authority.score(graph_tidy)$vector*150)
# nodes
networkD3::forceNetwork(Links = edges,
Nodes = nodes,
Source = "from",
Target = "to",
NodeID = "label",
Group = "id",
opacity = 1,
fontSize = 14,
zoom = TRUE,
Value = "N",
Nodesize = "nodesize",
opacityNoHover = TRUE
)
# Warning message:
# “It looks like Source/Target is not zero-indexed. This is required in JavaScript and so your plot may not render.”
# Nodes is a tbl_df. Converting to a plain data frame.
我看到了一个互动网络.
我可以移动并单击这些点(但在此处打印)。
没有分位数滤波器的相同图形:with(在开头):data2 <- data
将权重作为 id 和标签的一部分(图表摘录):
它是一个可缩放的矢量图形 (SVG):
<div class="forceNetwork html-widget html-fill-item-overflow-hidden html-fill-item html-widget-static-bound" id="htmlwidget-380bfc22386ecfa1b181" style="width:960px;height:500px;">
<svg width="960" height="500" pointer-events="all">
<g class="zoom-layer">
<g>
<line class="link" style="stroke: rgb(102, 102, 102); opacity: 0.25; stroke-width: 0.00216273px;" x1="410.4125002318474" y1="235.5820858923744" x2="374.73641851106566" y2="252.4766163768353"></line>
...
</g>
</svg>
</div>
链接:
评论
rthreejs
似乎还不错:在3D中!:)rthreejs