跟着Nature Communications學作圖:R語言circlize包做漂亮的弦圖

論文

A latitudinal gradient of deep-sea invasions for marine fishes

https://www.nature.com/articles/s41467-023-36501-4

s41467-023-36501-4.pdf

論文中對應的圖實現的代碼都有,鏈接是

https://github.com/stfriedman/Depth-transitions-paper

裏面有個弦圖很好看,在論文中對應的是figure3, 對應畫圖代碼是上面鏈接中的figure2

論文中的作圖數據是沒有提供的,這裏我就隨便構造一個數據,能夠把論文中提供的代碼運行通就可以

示例數據集

讀取數據集

library(readxl)

dat<-read_excel("data/20230301/20230301.xlsx")
dat

作圖代碼

circos.clear()
circos.par(start.degree = 90, 
           #gap.degree = 4, 
           points.overflow.warning = FALSE,
           gap.after=c("A1"=5,"A2"=5,"A3"=15,
                       "B1"=5,"B2"=5,"B3"=15,
                       "D1"=5,"D2"=5,"D3"=15))
grid.col<-c("A1"="#B0E0E6","A2"="#2C8EB5","A3"="#16465B",
            "B1"="#B0E0E6","B2"="#2C8EB5","B3"="#16465B",
            "D1"="#B0E0E6","D2"="#2C8EB5","D3"="#16465B")
group<-c("A1"="A","A2"="A","A3"="A",
         "B1"="B","B2"="B","B3"="B",
         "D1"="D","D2"="D","D3"="D")
chordDiagram(dat,
             grid.col = grid.col,
             col = rand_color(nrow(dat)),
             group = group,
             transparency = 0.25,
             directional = 1,
             direction.type = c("arrows", "diffHeight"), 
             diffHeight  = -0.04,
             annotationTrack = "grid", 
             annotationTrackHeight = c(0.08, 0.1),
             link.arr.type = "big.arrow", 
             # link.sort = TRUE, 
             # link.decreasing = TRUE,
             link.largest.ontop = TRUE,
             preAllocateTracks = list(
               track.height = 0.1,
               track.margin = c(0.01, 0)
             ))

circos.trackPlotRegion(
  track.index = 2, 
  bg.border = NA, 
  panel.fun = function(x, y) {
    
    xlim = get.cell.meta.data("xlim")
    #sector.index = get.cell.meta.data("sector.index")
    sector.index = gsub("[a-z]+_", "", get.cell.meta.data("sector.index"))
    
    # Add names to the sector. 
    circos.text(
      x = mean(xlim), 
      y = 0.5, 
      col = "white",
      labels = sector.index, 
      facing = "bending", 
      cex = 1,
      niceFacing = TRUE
    )
  }
)

names(group[7:9])
i<-1
highlight.sector(names(group[7:9]), track.index = 1, facing = "bending", font = 2,
                 col = ifelse(i == 1, "#E6AE48FF", "#E6AE483a"),
                 border = ifelse(i == 1, TRUE, FALSE), 
                 lwd = ifelse(i == 1, 2, 0.01),
                 text = "tropical", cex = 1.5, text.col = "white", niceFacing = TRUE)

highlight.sector(names(group[4:6]), track.index = 1, facing = "bending", font = 2,
                 border = ifelse(i == 2, TRUE, FALSE),
                 lwd = ifelse(i == 2, 2, 0.01),
                 col = ifelse(i == 2, "#20A486", "#20A4863a"),
                 text = "temperate", cex = 1.5, text.col = "white", niceFacing = TRUE)

highlight.sector(names(group[1:3]), track.index = 1, facing = "bending", font = 2,
                 col = ifelse(i == 3, "#472D7B", "#472D7B3a"),
                 border = ifelse(i == 3, TRUE, FALSE),
                 lwd = ifelse(i == 3, 2, 0.01),
                 text = "polar", cex = 1.5, text.col = "white", niceFacing = TRUE)
}

代碼裏有很多參數,這裏有的我也不太清楚是用來做什麼的,先記錄實現代碼,後續如果有需求做這個圖,再來研究具體參數的意思

https://jokergoo.github.io/circlize_book/book/the-chorddiagram-function.html 做這個圖可以參考這個鏈接

示例數據和代碼可以給推文點贊,然後點擊在看,最後留言獲取

歡迎大家關注我的公衆號

小明的數據分析筆記本

小明的數據分析筆記本 公衆號 主要分享:1、R語言和python做數據分析和數據可視化的簡單小例子;2、園藝植物相關轉錄組學、基因組學、羣體遺傳學文獻閱讀筆記;3、生物信息學入門學習資料及自己的學習筆記!

微信公衆號好像又有改動,如果沒有將這個公衆號設爲星標的話,會經常錯過公衆號的推文,個人建議將 小明的數據分析筆記本 公衆號添加星標,添加方法是

點開公衆號的頁面,右上角有三個點

點擊三個點,會跳出界面

直接點擊 設爲星標 就可以了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章