首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调整R中的highlight.sector()宽度和位置-弦图(圆环包)

调整R中的highlight.sector()宽度和位置-弦图(圆环包)
EN

Stack Overflow用户
提问于 2018-02-10 16:57:27
回答 2查看 2.3K关注 0票数 3

我需要一些帮助调整突出显示的扇区的chordDiagram()从圆圈包。

我正在处理渔业登陆数据。渔船在一个港口(国内港口PORT_DE)开始他们的旅行,在另一个港口(登陆港口PORT_LA)降落他们的渔获物。我的工作是扇贝活重吨(登陆SCALLOP_W)。下面是一个简单的dataframe示例:

代码语言:javascript
复制
        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)标记。下面的例子。

代码语言:javascript
复制
   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设置)。这是我现在的和弦图,

我几乎制作了我想要的东西,除了按国家强调行业的最后一点。我试图使用highlight.sector()来突出显示同一个国家的端口,但是我不能调整突出显示扇区的宽度或位置。目前,国家部门与所有其他标签重叠。例子如下:

请注意,这两个数字之间有不同的颜色,因为颜色是随机生成的。

你能帮我做最后的调整吗?

产生下列数字的代码:

代码语言:javascript
复制
# 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()
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-02-20 14:15:09

我已经尝试了一段时间来寻找解决方案。现在,我已经通过调整默认的轨道边距和默认的轨道高度来调整highlight.sector()的位置和宽度。

我在初始化track.margin步骤时指定了track.heightcircos.par()参数。

最终产品如下所示。在答案的结尾处编码。

代码语言:javascript
复制
# 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()
票数 3
EN

Stack Overflow用户

发布于 2019-04-18 19:43:19

如果你像我一样有点强迫症,你也可以把sector.numeric.index添加到扇区,并在旁边添加一个传奇,所以它看起来只是有点像,只是我的2分钱!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48723254

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档