ggraph R中节点的具有特定比例的线性图
linear graph with specific scale for nodes in ggraph R
我正在尝试使用出色的 ggraph 库来描绘一些非常难以描绘科学作品的相互关系。具体来说,我想展示基因位点中的 SNP-SNP 相互作用。如果我将相互作用绘制为图形的弯曲节点,那将是非常好的,其中 SNP 根据其遗传位置以线性方式定位。 ggraph 库中的 geom_edge_arc() 美学将是理想的。但是,我无法根据位置对节点进行排序。
这是一个例子
library(igraph)
library(tidyverse)
library(ggraph)
set.seed(10)
nodes <- tibble(nodes = paste("SNP",seq(1:10)), pos = sample(c(10000:20000),10))
edges <- expand.grid(nodes$nodes,nodes$nodes) %>%
mutate(interaction = rnorm(100)) %>%
filter(abs(interaction)>1)
gr <- graph_from_data_frame(edges, vertices = nodes)
ggraph(gr, 'linear', circular=F) +
geom_edge_arc(aes(edge_width=interaction))
节点在这里均匀分布,如"factors"。但是,我想将它们放置在 pos
变量指定的 x 坐标上(这又成为节点的一个属性)。将 + geom_node_point(aes(x=pos))
添加到 ggplot 对象不会导致正确的渲染。我可能也可以用 "basic" ipgraph 来绘图,但我喜欢 ggraph 和 ggplot2,用它来绘图是一种优雅而简单的方法。
谨致问候,并提前致谢,
罗伯特
不确定这是否仍然相关,但有两种方法可以解决这个问题。
如@axeman 所述,您可以使用 manual
布局,并基本上将 x
和 y
坐标传递给它:
ggraph(gr,
layout = 'manual',
node.position = data_frame(y = rep(0, length(nodes$pos)), x = nodes$pos)) +
geom_edge_arc(aes(edge_width=interaction))
另一种方法是覆盖 geom_edge_arc
中的 x
aes。为了能够将节点属性传递给 aes
我们需要使用 geom_edge_arc2
:
ggraph(gr, 'linear', circular=F) +
geom_edge_arc2(aes(edge_width=interaction, x = node.pos))
由 reprex package (v0.2.0) 创建于 2018-05-30。
我正在尝试使用出色的 ggraph 库来描绘一些非常难以描绘科学作品的相互关系。具体来说,我想展示基因位点中的 SNP-SNP 相互作用。如果我将相互作用绘制为图形的弯曲节点,那将是非常好的,其中 SNP 根据其遗传位置以线性方式定位。 ggraph 库中的 geom_edge_arc() 美学将是理想的。但是,我无法根据位置对节点进行排序。
这是一个例子
library(igraph)
library(tidyverse)
library(ggraph)
set.seed(10)
nodes <- tibble(nodes = paste("SNP",seq(1:10)), pos = sample(c(10000:20000),10))
edges <- expand.grid(nodes$nodes,nodes$nodes) %>%
mutate(interaction = rnorm(100)) %>%
filter(abs(interaction)>1)
gr <- graph_from_data_frame(edges, vertices = nodes)
ggraph(gr, 'linear', circular=F) +
geom_edge_arc(aes(edge_width=interaction))
节点在这里均匀分布,如"factors"。但是,我想将它们放置在 pos
变量指定的 x 坐标上(这又成为节点的一个属性)。将 + geom_node_point(aes(x=pos))
添加到 ggplot 对象不会导致正确的渲染。我可能也可以用 "basic" ipgraph 来绘图,但我喜欢 ggraph 和 ggplot2,用它来绘图是一种优雅而简单的方法。
谨致问候,并提前致谢,
罗伯特
不确定这是否仍然相关,但有两种方法可以解决这个问题。
如@axeman 所述,您可以使用 manual
布局,并基本上将 x
和 y
坐标传递给它:
ggraph(gr,
layout = 'manual',
node.position = data_frame(y = rep(0, length(nodes$pos)), x = nodes$pos)) +
geom_edge_arc(aes(edge_width=interaction))
另一种方法是覆盖 geom_edge_arc
中的 x
aes。为了能够将节点属性传递给 aes
我们需要使用 geom_edge_arc2
:
ggraph(gr, 'linear', circular=F) +
geom_edge_arc2(aes(edge_width=interaction, x = node.pos))
由 reprex package (v0.2.0) 创建于 2018-05-30。