jokergoo / circlize

Circular visualization in R
http://jokergoo.github.io/circlize_book/book/
Other
959 stars 142 forks source link

How bind the start and end of link with same target and source value #151

Closed ewijaya closed 4 years ago

ewijaya commented 4 years ago

I have the following data, using Circlize package:

library(circlize)
library(tidyverse)

df <- structure(list(ligand = c(
  "  Cxcr4", "  Cd44", "  Cxcr4", "   Cxcr4",
  "   Csf2rb", "   Plaur", "    Plaur", "    Cxcr4", "    Csf3r",
  "    Sell", "     Tnfrsf1b", "      Sell", "       Csf2rb", "       Tnfrsf1b",
  "       Csf2rb", "       Il1r2", "       Plaur", "       Calm1",
  "       Cd44", "        Ptafr", "         Il1r2", "         Calm1",
  "          Cxcr2", "          Cxcr2"
), receptor = c(
  "  Dsg2",
  "  Itgb1", "  Cxcl10", "  Cxcl10", "  Itgb1", "  Itgb1", "  Agt",
  "  Csf1", "  Csf1", "  Icam1", "   Calm1", "  Calm1", "  Tnf",
  "  App", "  Il1b", "  Tnf", "  Il1b", "  Tnf", "  Mmp9", "  Anxa1",
  "  Il1b", "  Il1b", "  Cxcl10", "  Calr"
)), class = c(
  "tbl_df",
  "tbl", "data.frame"
), row.names = c(NA, -24L))

gaps <- c(
  0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
  0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 3,
  0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25,
  0.25, 0.25, 3
)

grid_col <- c(`  Cxcr4` = "#8DD3C7", `  Cd44` = "#8DD3C7", `   Cxcr4` = "#FFFFB3", 
`   Csf2rb` = "#FFFFB3", `   Plaur` = "#FFFFB3", `    Plaur` = "#BEBADA", 
`    Cxcr4` = "#BEBADA", `    Csf3r` = "#BEBADA", `    Sell` = "#BEBADA", 
`     Tnfrsf1b` = "#FB8072", `      Sell` = "#80B1D3", `       Csf2rb` = "#FDB462", 
`       Tnfrsf1b` = "#FDB462", `       Il1r2` = "#FDB462", `       Plaur` = "#FDB462", 
`       Calm1` = "#FDB462", `       Cd44` = "#FDB462", `        Ptafr` = "#B3DE69", 
`         Il1r2` = "#FCCDE5", `         Calm1` = "#FCCDE5", `          Cxcr2` = "#D9D9D9", 
`  Dsg2` = "#000000", `  Itgb1` = "#000000", `  Cxcl10` = "#000000", 
`  Agt` = "#000000", `  Csf1` = "#000000", `  Icam1` = "#000000", 
`   Calm1` = "blue", `  Calm1` = "#000000", `  Tnf` = "#000000", 
`  App` = "#000000", `  Il1b` = "#000000", `  Mmp9` = "#000000", 
`  Anxa1` = "#000000", `  Calr` = "#000000")

order <- c("  Cxcr4", "  Cd44", "   Cxcr4", "   Csf2rb", "   Plaur", "    Plaur", 
"    Cxcr4", "    Csf3r", "    Sell", "     Tnfrsf1b", "      Sell", 
"       Csf2rb", "       Tnfrsf1b", "       Il1r2", "       Plaur", 
"       Calm1", "       Cd44", "        Ptafr", "         Il1r2", 
"         Calm1", "          Cxcr2", "   Calm1", "  Dsg2", "  Itgb1", 
"  Cxcl10", "  Agt", "  Csf1", "  Icam1", "  Calm1", "  Tnf", 
"  App", "  Il1b", "  Mmp9", "  Anxa1", "  Calr")

And with this code:

circos.par(gap.degree = gaps)
chordDiagram(df,
             directional = TRUE, 
             order = order, 
             link.sort = TRUE,
             link.decreasing = FALSE,
             grid.col = grid_col,
             diffHeight = 0.005,
             direction.type = c("diffHeight", "arrows"),
             # link.overlap = TRUE,
             link.arr.type = "big.arrow",
             annotationTrack = "grid",
             preAllocateTracks = list(track.height = 0.075)
)
# we go back to the first track and customize sector labels
circos.track(track.index = 1, panel.fun = function(x, y) {
  circos.text(CELL_META$xcenter, CELL_META$ylim[1], CELL_META$sector.index,
              facing = "clockwise", niceFacing = TRUE,
              adj = c(0, 0.55),
              cex = 0.5
  )
}, bg.border = NA)

circos.clear()

I can make this plot:

enter image description here

As stated in the plot above, how cand I bind the start and end of the link with the same target and source value?

So for example, Cxcr1 will only have one block as the sender with gray color.

jokergoo commented 4 years ago

First, you need remove the empty spaces in df.

df[[1]] = gsub("^\\s+|\\s+$", "", df[[1]])
df[[2]] = gsub("^\\s+|\\s+$", "", df[[2]])

For you question, you just append an interaction of "Cxcr2 Cxcr2" to df.

# I converted df to a data frame
df2 = rbind(df, data.frame(ligand = "Cxcr2", receptor = "Cxcr2"))
chordDiagram(df2)

image

You can also have a look at this link: https://jokergoo.github.io/circlize_book/book/the-chorddiagram-function.html#self-links