0

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')
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87

1 Answers1

1

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

enter image description here

Note though that where your numbers are close together, there will be an overlap of labels. This is not a trivial problem to solve.

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87