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

axzmvihb  于 2022-12-25  发布在  其他
关注(0)|答案(1)|浏览(128)

我只想在它们有连接时标注图。我的数据是

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

密码是

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')
mkshixfv

mkshixfv1#

如果您希望数字只出现在图表的节点上,则可以执行以下操作:

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')

不过要注意,在数字靠得很近的地方,标签会重叠,这不是一个容易解决的问题。

相关问题