Search code examples
rcolorsarc-diagram

Edge coloring in Arc Diagram


I have a dataframe with 3 columns and I create an arc diagram ( source x target). I don't know how to colour my edge with st_disease.

Here is my code :

links <- data.frame( source = c("Convalescent plasma", "Convalescent whole blood", "Gentamicin", "Interferon β-1a", "Lopinavir/ritonavir and recombinant IFN-β1b", "MAb114", "Plasma", "Plasma", "REGN-EB3", "Remdesivir", "Ribavirin", "Ribavirin", "Ribavirin", "Ribavirin", "Ribavirin", "Ribavirin", "Tecovirimat", "ZMapp"), target = c("No active treatment", "No active treatment", "Doxycycline", "No active treatment", "No active treatment", "ZMapp", "No active treatment", "No active treatment", "ZMapp", "ZMapp", "No active treatment", "No active treatment", "No active treatment", "No active treatment", "No active treatment", "No active treatment", "No active treatment", "Favipiravir"), st_disease = c("Ebola", "Ebola", "Plague", "Ebola", "MERS", "Ebola", "Lassa fever", "Lassa fever", "Ebola", "Ebola", "Nipah", "Crimean Congo Haemorrhagic Fever", "Crimean Congo Haemorrhagic Fever", "Lassa fever", "Lassa fever", "Lassa fever", "Mpox", "Ebola") )

mygraph <- graph_from_data_frame(links) p2 <- ggraph(mygraph, layout="linear") + geom_edge_arc0(edge_colour="#69b3a2", edge_alpha=0.3, edge_width=1.5) + geom_node_point( color="#69b3a2", size=5, position = "identity") + geom_node_text( aes(label=name), repel = TRUE, size=4, color="black", nudge_y=-0.5 ,angle=70, vjust=1) + scale_edge_color_manual(values = col)+ theme_void() + theme( legend.position="none", # plot.margin=unit(rep(2,4), "cm") plot.margin=unit(c(4,2,2,2), "cm"))+
coord_cartesian(clip = "off")

p2

I tried to change edge_colour but I dot not understand why it's asking a Aesthetics of length 72.


Solution

  • I am not sure what the scale_edge_color_manual(values = col) in your code is supposed to be. Assuming it is a vector defining the color palette , I do not see it defined. You can create the color palette first and then use scale_edge_color_manual to use that palette for your plot.

    library(ggraph)
    
    
    links <- data.frame(
      source = c("Convalescent plasma", "Convalescent whole blood", "Gentamicin", "Interferon β-1a", 
                 "Lopinavir/ritonavir and recombinant IFN-β1b", "MAb114", "Plasma", "Plasma", "REGN-EB3", 
                 "Remdesivir", "Ribavirin", "Ribavirin", "Ribavirin", "Ribavirin", "Ribavirin", "Ribavirin", 
                 "Tecovirimat", "ZMapp"), 
      target = c("No active treatment", "No active treatment", "Doxycycline", "No active treatment", 
                 "No active treatment", "ZMapp", "No active treatment", "No active treatment", "ZMapp", 
                 "ZMapp", "No active treatment", "No active treatment", "No active treatment", 
                 "No active treatment", "No active treatment", "No active treatment", "No active treatment", 
                 "Favipiravir"), 
      st_disease = c("Ebola", "Ebola", "Plague", "Ebola", "MERS", "Ebola", "Lassa fever", "Lassa fever", 
                     "Ebola", "Ebola", "Nipah", "Crimean Congo Haemorrhagic Fever", 
                     "Crimean Congo Haemorrhagic Fever", "Lassa fever", "Lassa fever", "Lassa fever", 
                     "Mpox", "Ebola")
    )
    
    mygraph <- graph_from_data_frame(links)
    
    # Create a color palette for the st_disease column
    disease_colors <- setNames(
      c("#e41a1c", "#377eb8", "#4daf4a", "#984ea3", "#ff7f00", "#ffff33", "#a65628", "#f781bf"),
      unique(links$st_disease)
    )
    
    
    p2 <- ggraph(mygraph, layout = "linear") +
      geom_edge_arc(aes(edge_colour = st_disease), edge_width = 1.5, edge_alpha = 0.3) +
      geom_node_point(color = "#69b3a2", size = 5) +
      geom_node_text(aes(label = name), repel = TRUE, size = 4, color = "black", 
                     nudge_y = -0.5, angle = 70, vjust = 1) +
      scale_edge_color_manual(values = disease_colors) +
      theme_void() +
      theme(legend.position = "right", plot.margin = unit(c(4, 2, 2, 2), "cm")) +
      coord_cartesian(clip = "off")
    
    p2