简体   繁体   English

我如何在 r 圆形图中标记仅连接的节点

[英]how can i label only connected node in r circular plot

I want to label the plot only if they have conection.只有当他们有联系时,我才想给情节贴上标签。 My data is我的数据是

df <- data.frame(col1 = c(2, 26, 26, 27, 27, 28, 28, 28),
             col2 = c(94, 146, 175, 213, 213, 55, 247, 263))

And code is代码是

library(tidygraph)
library(ggraph)

df %>%
bind_rows(data.frame(col1 = 1:360, col2 = 1:360)) %>%
arrange(col1) %>%
as_tbl_graph() %>%
activate(edges) %>%
mutate(diff = as.character(abs(from - to))) %>%
ggraph(layout = "linear", circular = TRUE) +
geom_edge_arc(aes(colour = diff), lwd = 1) +
ggforce::geom_circle(aes(x0 = 0, y0 = 0, r = 1.024), size = 15, 
                   color = '#fff450') +
geom_node_text(aes(label = ifelse(as.numeric(name) %% 10 == 0, name, ""),
                 angle = -as.numeric(name) + 90)) +
coord_equal() +
theme_graph() +
theme(plot.background = element_rect(fill = 'black'),
    legend.position = 'none')

If you want the numbers just at the nodes of your graph, then you can do:如果您只想在图形的节点处使用数字,则可以执行以下操作:

library(tidyverse)
library(tidygraph)
library(ggraph)

df %>%
  bind_rows(data.frame(col1 = 1:360, col2 = 1:360)) %>%
  arrange(col1) %>%
  as_tbl_graph() %>%
  activate(edges) %>%
  mutate(diff = as.character(abs(from - to))) %>%
  ggraph(layout = "linear", circular = TRUE) +
  geom_edge_arc(aes(colour = diff), lwd = 1) +
  ggforce::geom_circle(aes(x0 = 0, y0 = 0, r = 1.024), size = 15, 
                       color = '#fff450') +
  geom_node_text(aes(label = ifelse(name %in% unlist(df), name, ""),
                     angle = ifelse((-as.numeric(name) + 90) < -90,
                     -as.numeric(name) - 90, -as.numeric(name) + 90))) +
  coord_equal() +
  theme_graph() +
  theme(plot.background = element_rect(fill = 'black'),
        legend.position = 'none')

在此处输入图像描述

Note though that where your numbers are close together, there will be an overlap of labels.请注意,虽然您的数字靠得很近,但标签会重叠。 This is not a trivial problem to solve.这不是一个需要解决的小问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM