【问题标题】:Specify node shape and label in a bipartite network with ggraph使用 ggraph 在二分网络中指定节点形状和标签
【发布时间】:2021-07-10 09:14:13
【问题描述】:

我想用 ggraph 绘制一个相邻的矩阵。 我的数据是这样的

set.seed(43);
check <- matrix(rnorm(10), nrow = 5,ncol = 4, dimnames = list(c("AL","CH","CZ","DN","SC"), c("GR","EN","GE","FR")))

            GR          EN          GE          FR
AL -0.03751376 -0.27743280 -0.03751376 -0.27743280
CH -1.57460441  0.38643441 -1.57460441  0.38643441
CZ -0.48596752 -0.06040412 -0.48596752 -0.06040412
DN  0.46518623 -0.68617976  0.46518623 -0.68617976
SC -0.90409807 -1.90613679 -0.90409807 -1.90613679

我想使用二分网络绘制交互,但我发现很难在顶部和底部的 x 轴上指定标签。 我也想改变节点的形状。我想在顶部看到一个蓝色方块,在底部看到一个绿色圆圈。

ggraph(check, layout = "bipartite") + 
  geom_edge_link0(aes(edge_width = weight),edge_colour = "grey66") +
  geom_node_point()

非常感谢任何帮助或评论。

【问题讨论】:

  • 能否包含您用来生成图形的代码?
  • 谢谢@teunbrand。我刚刚添加了它

标签: r ggplot2 dplyr tidyverse ggraph


【解决方案1】:

如果在将邻接矩阵传递给绘图之前创建布局,则可以检查其中的变量。

library(ggraph)
#> Loading required package: ggplot2
set.seed(43);
check <- matrix(rnorm(10), nrow = 5,ncol = 4, 
                dimnames = list(c("AL","CH","CZ","DN","SC"), 
                                c("GR","EN","GE","FR")))

(layout <- create_layout(check, "bipartite"))
#>     x y  type name .ggraph.orig_index circular .ggraph.index
#> 1 0.5 1 FALSE   AL                  1    FALSE             1
#> 2 1.5 1 FALSE   CH                  2    FALSE             2
#> 3 2.5 1 FALSE   CZ                  3    FALSE             3
#> 4 3.5 1 FALSE   DN                  4    FALSE             4
#> 5 4.5 1 FALSE   SC                  5    FALSE             5
#> 6 1.0 0  TRUE   GR                  6    FALSE             6
#> 7 2.0 0  TRUE   EN                  7    FALSE             7
#> 8 3.0 0  TRUE   GE                  8    FALSE             8
#> 9 4.0 0  TRUE   FR                  9    FALSE             9

我们可以在上面的布局中看到type 变量似乎编码了二分图的哪一部分在顶部,哪一部分在底部。因此,我们可以使用节点层的映射,让shapecolour 依赖于type 变量。

g <- ggraph(layout) + 
  geom_edge_link0(aes(edge_width = weight),edge_colour = "grey66") +
  geom_node_point(aes(shape = type, colour = type), size = 6) +
  scale_colour_manual(values = c("blue", "green")) +
  scale_shape_manual(values = c(15, 19))
g

在 x 轴上获取正确的标签可能有点麻烦,但基本上您可以从布局中提取中断和标签信息。

g + scale_x_continuous(
  breaks = layout$x[!layout$type],
  labels = layout$name[!layout$type],
  position = "top",
  sec.axis = dup_axis(
    breaks = layout$x[layout$type],
    labels = layout$name[layout$type]
  )
) +
  theme(axis.text = element_text())

reprex package (v1.0.0) 于 2021-07-10 创建

请注意,这可能不适用于任何图,但由于特别是二分图的属性,它可以工作。标记节点的更通用策略是使用geom_node_text()

g + geom_node_text(aes(label = name, 
                       y = y + ifelse(type, -0.05, 0.05)), 
                   vjust = "outward")

【讨论】:

  • 亲爱的 teunbrand, 非常感谢您的回答。太好了。当我将您的代码用于我的实际数据时,我发现了困难。设置辅助 x 轴尤其困难。这与您的代码无关。如果你想看看我会很感激stackoverflow.com/questions/68337622/…
猜你喜欢
  • 2018-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-08
  • 2015-09-14
  • 2020-11-26
  • 2023-02-13
  • 2022-07-08
相关资源
最近更新 更多