簡體   English   中英

調整 highlight.sector() 寬度和位置 - R 中的和弦圖(circlize 包)

[英]Adjust highlight.sector() width and placement - Chord diagram (circlize package) in R

我需要一些幫助來調整 circlize 包中chordDiagram()突出顯示部分。

我正在處理漁業上岸數據。 漁船在一個港口(母港PORT_DE )開始PORT_DE ,然后在另一個港口(登陸港PORT_LA )上岸。 我正在處理以噸為單位的活重扇貝(着陸SCALLOP_W )。 這是數據框的一個簡單示例:

        PORT_DE  PORT_LA SCALLOP_W
1      Aberdeen Aberdeen       116
2        Barrow   Barrow        98
3       Douglas   Barrow       127
4 Kirkcudbright   Barrow       113
5       Brixham  Brixham        69
6        Buckie   Buckie       180

每個端口( Name_short )通過區域(標記為Region_lb ),以及由國家( Country_lb )。 下面舉例。

   Name_short Country_lb      Region_lb
1   Scalloway   Scotland Shetland Isles
2   Scrabster   Scotland    North Coast
3      Buckie   Scotland    Moray Firth
4 Fraserburgh   Scotland    Moray Firth
5    Aberdeen   Scotland     North East

使用circilze包,我生成了一個定制的chordDiagram來可視化端口之間的着陸流。 我已經通過調整扇區之間的間距調整了大部分設置,包括對同一國家的端口進行分組(請參閱gap.after setting)。 這是我的和弦圖的當前形式,

港口之間登陸的流程圖(弦)

除了最后按國家/地區突出顯示行業外,我幾乎已經制作了我想要的東西。 我正在嘗試使用highlight.sector()來突出顯示同一國家/地區的端口,但我無法調整突出顯示扇區的寬度或位置。 目前,國家部門與所有其他標簽重疊。 下面的例子:

在此處輸入圖片說明

請注意,由於顏色是隨機生成的,因此兩個圖形之間存在不同的顏色。

你能幫我做最后的調整嗎?

生成如下圖的代碼:

# calculate gaps by country; 
# 1 degree between ports, 10 degree between countries
gaps <- rep(1, nrow(port_coords))
gaps[cumsum(as.numeric(tapply(port_coords$Name_short, port_coords$Country_lb, length)))] <- 10

# edit initialising parameters
circos.par(canvas.ylim=c(-1.5,1.5), # edit  canvas size 
           gap.after = gaps, # adjust gaps between regions
           track.margin = c(0.01, 0)) # adjust bottom and top margin 
                                      # (blank area out of the plotting regio)

# Plot chord diagram
chordDiagram(m,

             # manual order of sectors
             order = port_coords$Name_short,

             # plot only grid (no labels, no axis)
             annotationTrack = "grid", 
             preAllocateTracks = 1, 

             # adjust grid width and spacing
             annotationTrackHeight = c(0.03, 0.01), 

             # add directionality
             directional=1, 
             direction.type = c("diffHeight", "arrows"), 
             link.arr.type = "big.arrow",

             # adjust the starting end of the link
             diffHeight = -uh(1, "mm"),

             # adjust height of all links
             h.ratio = 0.8,

             # add link border
             link.lwd = 1, link.lty = 1, link.border="gray35"

             )

# add labels and axis manually
circos.trackPlotRegion(track.index = 1, panel.fun = function(x, y) {
  xlim = get.cell.meta.data("xlim")
  ylim = get.cell.meta.data("ylim")
  sector.name = get.cell.meta.data("sector.index")

  # print labels & text size (cex)
  circos.text(mean(xlim), ylim[1] + .7, sector.name, 
              facing = "clockwise", niceFacing = TRUE, adj = c(0, 0.5), cex=0.6)

  # print axis
  circos.axis(h = "top", labels.cex = 0.5, major.tick.percentage = 0.2, 
              sector.index = sector.name, track.index = 2)
}, bg.border = NA)

# add additional track to enhance the visual effect of different groups
# Scotland
highlight.sector(port_coords$Name_short[which(port_coords$Country_lb == "Scotland")],
                 track.index = 1, col = "blue",
                 text = "Scotland", cex = 0.8, text.col = "white", niceFacing = TRUE)
# England
highlight.sector(port_coords$Name_short[which(port_coords$Country_lb == "England")],
                 track.index = 1, col = "red",
                 text = "England", cex = 0.8, text.col = "white", niceFacing = TRUE)
# Wales
highlight.sector(port_coords$Name_short[which(port_coords$Country_lb == "Wales")],
                 track.index = 1, col = "forestgreen",
                 text = "Wales", cex = 0.8, text.col = "white", niceFacing = TRUE)
# Isle of Man
highlight.sector(port_coords$Name_short[which(port_coords$Country_lb == "Isle of Man")],
                 track.index = 1, col = "darkred",
                 text = "Isle of Man", cex = 0.8, text.col = "white", niceFacing = TRUE)
# Rep. Ireland
highlight.sector(port_coords$Name_short[which(port_coords$Country_lb == "Rep. Ireland")],
                 track.index = 1, col = "darkorange2",
                 text = "Ireland", cex = 0.8, text.col = "white", niceFacing = TRUE)
# N.Ireland
highlight.sector(port_coords$Name_short[which(port_coords$Country_lb == "N.Ireland")],
                 track.index = 1, col = "magenta4",
                 text = "N. Ireland", cex = 0.8, text.col = "white", niceFacing = TRUE)

# re-set circos parameters
circos.clear()

我已經嘗試了一段時間來找到解決方案。 我現在已經通過調整默認軌道邊距和默認軌道高度來調整highlight.sector()的位置和寬度。

我通過在circos.par()步驟的初始化中指定track.margintrack.height參數來做到這一點。

最終產品如下所示。 代碼最后給出答案。

在此處輸入圖片說明

# calculate gaps by country; 
# 1 degree between ports, 10 degree between countries
gaps <- rep(1, nrow(port_coords))
gaps[cumsum(as.numeric(tapply(port_coords$Name_short, port_coords$Country_lb, length)))] <- 10

# edit initialising parameters
circos.par(canvas.ylim=c(-1.5,1.5), # edit  canvas size 
           gap.after = gaps, # adjust gaps between regions
           track.margin = c(0.01, 0.05), # adjust bottom and top margin
           # track.margin = c(0.01, 0.1)
           track.height = 0.05)

# Plot chord diagram
chordDiagram(m,

             # manual order of sectors
             order = port_coords$Name_short,

             # plot only grid (no labels, no axis)
             annotationTrack = "grid",
             # annotationTrack = NULL, 
             preAllocateTracks = 1, 

             # adjust grid width and spacing
             annotationTrackHeight = c(0.03, 0.01), 

             # add directionality
             directional=1, 
             direction.type = c("diffHeight", "arrows"), 
             link.arr.type = "big.arrow",

             # adjust the starting end of the link
             diffHeight = -uh(1, "mm"),

             # adjust height of all links
             h.ratio = 0.8,

             # add link border
             link.lwd = 1, link.lty = 1, link.border="gray35"

             # track.margin = c(0.01, 0.1)

             )

# Scotland
highlight.sector(port_coords$Name_short[which(port_coords$Country_lb == "Scotland")],
                 track.index = 1, col = "blue2",
                 text = "Scotland", cex = 1, text.col = "white", 
                 niceFacing = TRUE, font=2)
# England
highlight.sector(port_coords$Name_short[which(port_coords$Country_lb == "England")],
                 track.index = 1, col = "red2",
                 text = "England", cex = 1, text.col = "white", 
                 niceFacing = TRUE, font=2)
# Wales
highlight.sector(port_coords$Name_short[which(port_coords$Country_lb == "Wales")],
                 track.index = 1, col = "springgreen4",
                 text = "Wales", cex = 1, text.col = "white", 
                 niceFacing = TRUE, font=2)
# Isle of Man
highlight.sector(port_coords$Name_short[which(port_coords$Country_lb == "Isle of Man")],
                 track.index = 1, col = "orangered4",
                 text = "Isle of Man", cex = 1, text.col = "white", 
                 niceFacing = TRUE, font=2)
# Rep. Ireland
highlight.sector(port_coords$Name_short[which(port_coords$Country_lb == "Rep. Ireland")],
                 track.index = 1, col = "darkorange3",
                 text = "Ireland", cex = 1, text.col = "white", 
                 niceFacing = TRUE, font=2)
# N.Ireland
highlight.sector(port_coords$Name_short[which(port_coords$Country_lb == "N.Ireland")],
                 track.index = 1, col = "magenta4",
                 text = "NI", cex = 1, text.col = "white", 
                 niceFacing = TRUE, font=2)

# add labels and axis manually
circos.trackPlotRegion(track.index = 1, panel.fun = function(x, y) {
  xlim = get.cell.meta.data("xlim")
  ylim = get.cell.meta.data("ylim")
  sector.name = get.cell.meta.data("sector.index")

  # print labels & text size (cex)
  # circos.text(mean(xlim), ylim[1] + .7, sector.name, 
  #             facing = "clockwise", niceFacing = TRUE, adj = c(0, 0.5), cex=0.6)

  circos.text(mean(xlim), ylim[1] + 2, sector.name, 
              facing = "clockwise", niceFacing = TRUE, adj = c(0, 0.5), cex=0.6)

  # print axis
  circos.axis(h = "bottom", labels.cex = 0.5, 
              # major.tick.percentage = 0.05, 
              major.tick.length = 0.6,
              sector.index = sector.name, track.index = 2)
}, bg.border = NA)

# re-set circos parameters
circos.clear()

如果你和我一樣有點 ocd,你也可以在扇區中添加sector.numeric.index 並在旁邊添加一個圖例,這樣看起來更好一點,只有我的 2 美分!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM