How to reflect 3 relationships in a Chord Diagram and avoid label overlap?

293 Views Asked by At

I am trying to great a chord diagram that reflects a 3 way connection that goes from the individual sample to species to what label it falls under. However when I try to create my chord diagram some of my labels are overlapping and you cant see what is each part. This doesn't happen when I use a smaller sample amount like 20, but I have a large data set of 40 which leads to overlap. Also my third label "seafood" does not show up at all and just disappears when I want to show that all those species fall under same label

The example data I have is:

Sample  Species  Label
L1  Shark   Seafood
L2  Tuna    Seafood
L3  Shark   Seafood
L4  Shrimp  Seafood
L5  Crab    Seafood
L6  Tuna    Seafood
L7  Shrimp  Seafood
L8  Shark   Seafood
L9  Shark   Seafood
L10 Crab    Seafood
L11 Tuna    Seafood
L12 Shrimp  Seafood
L13 Crab    Seafood
L14 Crab    Seafood
L15 Shark   Seafood
L16 Tuna    Seafood
L17 Tuna    Seafood
L18 Shark   Seafood
L19 Shark   Seafood
L20 Shrimp  Seafood
L21 Shark   Seafood
L22 Tuna    Seafood
L23 Shark   Seafood
L24 Shrimp  Seafood
L25 Crab    Seafood
L26 Tuna    Seafood
L27 Shrimp  Seafood
L28 Shark   Seafood
L29 Shark   Seafood
L30 Crab    Seafood
L31 Tuna    Seafood
L32 Shrimp  Seafood
L33 Crab    Seafood
L34 Crab    Seafood
L35 Shark   Seafood
L36 Tuna    Seafood
L37 Tuna    Seafood
L38 Shark   Seafood
L39 Shark   Seafood
L40 Shrimp  Seafood

The code I have is: library(circlize)

col.pal = c(Sample.= "blue", Species = "red", Label = "green")

chordDiagram(Example, grid.col = col.pal)

The plot I get is: enter image description here

What I would like to get is this where there is a third spot where all the lines from species would then connect to the seafood and all labels are visible: enter image description here

Please let me know any tips! I am totally new to chord diagrams and pretty new to coding so im sure im missing something. I saw some people use matrices for their chord diagrams but when I tried to do this i received error: "error non-numeric argument to mathematical function".

1

There are 1 best solutions below

5
On

I'm not sure a chord diagram is right for this sort of relationship, even if it is pretty. Essentially it sounds like you want a hierarchical graph (i.e. a tree structure). You can still get this to be attractive, colorful and even circular. Here's an example using your data and the tidygraph and ggraph packages:

library(tidygraph)
library(ggraph)
library(extrafont)

df1 <- Example[2:1]
df1$Sample <- paste(df1$Species, df1$Sample)
df2 <- Example[3:2]
names(df2) <- names(df1)

as_tbl_graph(rbind(df1, df2)) %>%
  activate(nodes)%>%
  mutate(group = ifelse(grepl(' ', name), sub(' .*$', '', name), name),
         name = ifelse(grepl(' ', name), sub('^.* ', '', name), name)) %>%
  ggraph(layout = 'igraph', algorithm = 'tree', circular = TRUE) +
  geom_edge_arc(aes(color = factor(from)), width = 2, alpha = 0.3) +
  geom_node_circle(aes(r = nchar(name)/45, fill = group), color = NA) +
  geom_node_text(aes(label = name), family = "Roboto Condensed", fontface = 2,
                 size = 6, color = "gray30") +
  theme_graph() +
  coord_equal() +
  scale_edge_color_brewer(palette = 'Pastel2') +
  scale_fill_brewer(palette = 'Pastel2', 
                    limits = c('Shark', 'Tuna', 'Shrimp', 'Crab', 'Seafood')) +
  theme(legend.position = 'none')

enter image description here