gpt4 book ai didi

r - 在 ggraph/ggplot2 中的圆形布局之外放置几何标签

转载 作者:行者123 更新时间:2023-12-04 09:36:23 25 4
gpt4 key购买 nike

我正在尝试以圆形布局生成带有标记节点的交互网络图。我拼凑了一个功能脚本,显示了我想要的与 ggraph 的连接,但我无法弄清楚如何相对于圆形排列中的位置定位节点标签,因此它们不会与任何连接线重叠地点。有人在使用 igraph ( Placing vertex.label outside a circular layout in igraph ) 时针对同样的问题发布了一个非常好的解决方案;但我认为这不适用于 ggraph,因为计算节点位置的方式似乎不同。我更愿意为此使用 ggraph,因为它有一个内置的图例功能,让我的生活更轻松。这是我到目前为止:

library(igraph)
library(tidyverse)
library(ggraph)

# Create example matrix
id <- c("s01", "s02", "s03", "s04", "s05")
s01 <- c(0, 0, 0, 1, 1)
s02 <- c(0, 0, 1, 1, 1)
s03 <- c(1, 0, 0, 0, 1)
s04 <- c(0, 1, 0, 0, 0)
s05 <- c(0, 0, 0, 0, 0)
links <- data.frame(id, s01, s02, s03, s04, s05)
links <- column_to_rownames(links, var = "id")
links <- as.matrix(links)
expertise <- c("neuro", "micro", "phys", "phys", "neuro")
nodes <- data.frame(id, expertise)

# Generate igraph object
network <- graph_from_adjacency_matrix(links, weighted = NULL, mode = "undirected")

# Generate plot
ggraph(network, layout = "circle") + geom_edge_arc(color = "darkgray", strength = 0.15) +
geom_node_point(size = 8, aes(color = nodes$expertise)) + geom_node_text(aes(label = id), nudge_y = -0.2) +
theme_void()
这将生成以下图:
enter image description here
问题是像 s02 这样的标签在网络内部,这成为密集图的问题。
有没有办法将所有标签放在节点圈之外?非常感激任何的帮助!

最佳答案

这并不完全令人满意,因为它需要将绘图指定为中间步骤,因为 geom nudge参数不带函数,但您可以相对于节点 xy 坐标向外微调标签:

library(igraph)
library(ggplot2)
library(ggraph)

# Generate igraph object
network <- graph_from_adjacency_matrix(links, weighted = NULL, mode = "undirected")

# Generate plot
p <- ggraph(network, layout = "circle") +
geom_edge_arc(color = "darkgray", strength = 0.15) +
geom_node_point(size = 8, aes(color = nodes$expertise)) +
theme_void()

p +
geom_node_text(aes(label = id), nudge_x = p$data$x * .1, nudge_y = p$data$y * .1)
enter image description here

关于r - 在 ggraph/ggplot2 中的圆形布局之外放置几何标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62565723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com