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

2024-02-20

我需要一些帮助来调整突出显示的部分chordDiagram()来自 circlize 包。

我正在处理渔业上岸数据。渔船从一个港口出发(母港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环境)。这是我的和弦图的当前形式,

除了最后按国家突出显示部门之外,我几乎已经制作出了我想要的东西。我正在尝试使用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()通过调整默认轨道边距和默认轨道高度。

我已经通过指定这样做了track.margin and track.height初始化时的参数circos.par() step.

最终产品如下所示。代码在最后答案。

# 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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

调整highlight.sector() 宽度和位置 - R 中的和弦图(circlize 包) 的相关文章

  • 将自定义误差线添加到 seaborn regplot 和 residplot

    有没有一种方法可以将自定义错误栏添加到seaborn regplot和residplot中 就像使用yerr对matplotlib错误栏所做的那样 例子在这里 如果我只是添加 yrr 参数就会发生错误 import seaborn as s
  • 如何绘制每条线之间具有特定距离的图形

    实际上 我尝试绘制一个图形 但它将所有列 线 放在一起并显示 因此它不具有代表性 我尝试制作模拟数据并向您展示我如何绘制它 并向您展示我想要的内容 我不知道如何制作像下面所示的示例的数据 但我在这里做了什么 set seed 1 M lt
  • 更快的 %in% 运算符

    The 快速匹配 https cran r project org web packages fastmatch index html包实现了更快的版本match对于重复匹配 例如在循环中 set seed 1 library fastma
  • 排序因素与水平

    有人能解释一下 R 中 ordered 参数的用途吗 R says ordered逻辑标志来确定级别是否应被视为有序 按给定的顺序 所以如果我有一个名为名称的因素并设置ordered TRUE names lt factor c fred
  • 计算互相关函数?

    In R 我在用ccf or acf计算成对互相关函数 以便我可以找出哪个移位给我带来最大值 从它的外观来看 R给我一个标准化的值序列 Python 的 scipy 中是否有类似的东西 或者我应该使用fft模块 目前 我正在这样做 xcor
  • 不同 R/lme4 版本的单一拟合结果不匹配

    我试图将 R 版本 3 5 3 lme4 1 1 18 1 的随机效应估计与 R 版本 4 1 1 lme4 1 1 27 1 相匹配 然而 当存在奇异拟合时 这两个版本之间的随机效应存在微小差异 我对奇点警告很满意 但令人费解的是不同版本
  • 如何在ubuntu的conda环境中更改Rstudio中的R版本

    我在基本系统中安装了 R 4 3 和 Rstudio 在 conda 环境中安装了旧版本的 R 4 2 3 命令which R返回环境中安装的 R 的目录 home 用户 miniconda3 envs anndata2ri pip bin
  • 使用字符串中的变量名称访问变量值,R

    Intro 一个数据集有大量的age year变量 age 1990 age 1991 etc 我有一个字符串值数组length age years 表示这些变量 使得age years 1 回报 age 1990 etc Need 我想搜
  • 从 n,k 维矩阵数组中减去 n,k 维矩阵

    如果我有一个数组A A lt array 0 c 4 3 5 for i in 1 5 set seed i A i lt matrix rnorm 12 4 3 如果我有矩阵 B set seed 6 B lt matrix rnorm
  • 如何在 Caret 中绘制随机森林(护林员)树

    我生成了如下所示的随机森林树 并尝试绘制它 但出现错误 我在哪里犯了错误 我怎样才能以正确的方式绘制它 Actmodel lt train Activity Section Author data CB1 method ranger trC
  • R中的字典数据结构

    在 R 中 我有 例如 gt foo lt list a 1 b 2 c 3 如果我输入foo I get a 1 1 b 1 2 c 1 3 我怎样才能看透foo仅获取 键 列表 在这种情况下 a b c R 列表可以具有命名元素 因此可
  • 为什么数据帧上的 is.vector 不返回 TRUE?

    tl dr R 中的向量到底是什么 长版 R 中很多东西都是向量 例如 数字是长度为 1 的数值向量 is vector 1 1 TRUE 列表也是一个向量 is vector list 1 1 TRUE 好的 所以列表是一个向量 显然 数
  • R - 计算 bin 中特定值的数量

    我有一个如下所示的数据框 df Value lt c 1 1 0 2 1 3 4 0 0 1 2 0 3 0 4 5 2 3 0 6 Sl lt c 1 20 df lt data frame Sl Value gt df Sl Value
  • Quantmod 的简单功能不再起作用

    我明天要交论文 我收到了一条关于 quantmod 的非常奇怪的错误消息 这是我在过去几周使用这个包时从未遇到过的 我无法导入特定于道琼斯指数 DJI 的数据 我收到以下错误消息 getSymbols DJI src yahoo from
  • 列出 R 数据文件的内容而不加载

    我有时用print load myDataFile RData 当我加载数据文件时列出它的内容 有没有办法列出内容而不加载数据文件中包含的对象 我认为如果不加载对象就无法做到这一点 解决方案可能是使用包装器将 R 对象保存到save 该函数
  • R 中两个时间戳之间的左连接

    我的目标是执行左连接intervals哪里的bike id比赛和created at时间戳在records在 之间start and end in the intervals table gt class records 1 data ta
  • 如何在 data.table 中分组后使用条件计算行数

    我有以下数据框 dat lt read csv s1 s2 v1 v2 a b 10 20 a b 22 NA a b 13 33 c d 3 NA c d 4 5 NA c d 10 20 dat gt A tibble 6 x 4 gt
  • 在R中循环子文件夹

    我正在 R 环境中包含多个子文件夹的文件夹中工作 我想要循环遍历多个子文件夹 然后在每个子文件夹中调用 R 脚本来执行 我想出了下面的代码 但我的代码似乎添加了 到子文件夹列表 我收到错误 文件中的错误 文件名 r 编码 编码 无效的 描述
  • 实现 XGboost 自定义目标函数

    我正在尝试使用 XGboost 实现自定义目标函数 在 R 中 但我也使用 python 所以有关 python 的任何反馈也很好 我创建了一个返回梯度和粗麻布的函数 它工作正常 但是当我尝试运行 xgb train 时它不起作用 然后 我
  • 绘制 Cox 回归的 Kaplan-Meier 图

    我使用 R 中的以下代码设置了一个 Cox 比例风险模型来预测死亡率 添加协变量 A B 和 C 只是为了避免混淆 即年龄 性别 种族 但我们真正对预测变量 X 感兴趣 X 是一个连续变量 cox model lt coxph Surv t

随机推荐

  • R闪亮:将文本显示为多行代码

    我想在我闪亮的应用程序上显示一些 R 代码 因此 我用了verbatimTextOutput但我找不到换行和显示代码段落的方法 这个解决方案 在 R Shiny 中使用 renderText 输出多行文本 https stackoverfl
  • Java中有通配符这样的东西吗?

    我正在运行一个比较程序 此时它会进行直接的 字符串到字符串 比较 如果它们完全匹配 则输出它们是匹配的 好吧 我希望添加一个允许 相似性 的附加功能 例如 String em1 52494646 String em2 52400646 if
  • 从 Azure SQL DW 中更新?

    我在尝试执行 UPDATE FROM 查询时在 Azure SQL DW 中收到错误 错误是 UPDATE 和 DELETE 语句中的 FROM 子句不能包含子查询源或联接 这只是 SQL DW 特有的吗 除此之外 我认为这个查询没有任何问
  • 局部变量类型推断未被识别

    我安装了 JDK 10 来尝试新功能 但我对此非常着迷var 由于某种原因 即使IntelliJ 版本2018 1 中添加了JDK 以下代码仍然无法编译 说Java找不到符号var public class Variations publi
  • 尝试返回列表中的数字

    我对这里的代码有几个疑问 我想做的是编写一个函数 它接受 2 个输入 一个列表和一个选项 其中选项为 0 或 1 并返回列表中的数字列表 如果选项为 0 它将返回大于 5 或 小于 5 的数字 如果选项为 1 它将返回第一个列表中所有奇数的
  • 实体框架和多线程

    我们在设计多线程实体框架驱动的应用程序时遇到一些问题 需要一些指导 我们在不同的线程上创建实体 将实体添加到集合中 然后将集合数据绑定到各种 WPF 控件 ObjectContext 类不是线程安全的 因此管理它我们基本上有 2 个解决方案
  • 闪亮的 downloadHandler 超时

    我制作了一个闪亮的应用程序 需要下载比下载处理程序允许的时间更长的时间 在我下载的数据完成之前 我不断收到一条错误消息 提示 未收到数据 有什么方法可以增加闪亮服务器应用程序中 downloadHandler 的超时长度吗 连接的默认超时时
  • 使用 git diff 创建部署文件列表时出现问题

    我想使用类似以下命令来创建要部署的 tarball tar cjvf deploy tar bz2 git diff name only 0abc 1def 当我单独运行它时 内部 git diff 命令会生成一个包含相对路径的文件列表 不
  • 分析字母数字字符串的格式

    我正在尝试编写一个函数 该函数接受一个字符串 对其进行解析 然后返回另一个字符串 该字符串总结了原始字符串中连续字母或数字字符的数量 例如 字符串999aa45bbx会回来3N2A2N3A i e 3个数字 接下来是 2 个阿尔法 2 个数
  • 函数重载和函数指针

    函数的名称是指向该函数的指针 但在函数重载的情况下 两个函数的名称是相同的 那么这个名字指向哪个函数呢 这取决于上下文 否则它是模棱两可的 看这个例子 http www java2s com Code Cpp Function Assign
  • 使用正则表达式删除 HTML 标签

    我需要转换 text We had i fun i Look at a href http example com this photo a of Joe 编辑 文本中可能有多个链接 to text We had fun Look at t
  • 使用cin两次的问题

    这是代码 string str cin gt gt str cout lt lt first input lt
  • Jupyter Notebooks 中的 Python 版本和环境的 VSCode 问题

    Issue 我遇到的问题是 Python 的环境和版本与 VSCode 中的设置不匹配 并导致我尝试在 Jupyter 笔记本中使用的包出现问题 我使用的是安装了 Python 3 9 1 包括旧版本 和 Visual Studio Cod
  • IActionFilter 和 IAuthorizationFilter 之间的区别

    我只是想知道两者之间是否有什么区别IActionFilter and IAuthorizationFilter 我假设我们可以在下面实现相同的逻辑IActionFilter可能有IAuthorizationFilter 真的吗 Thanks
  • 从 C++ 中的 std::string 中删除空格

    在 C 中从字符串中删除空格的首选方法是什么 我可以循环遍历所有字符并构建一个新字符串 但有更好的方法吗 最好的办法就是使用算法remove if http en cppreference com w cpp algorithm remov
  • jQuery UI:日期选择器将年份范围下拉设置为 100 年

    使用日期选择器时 年份下拉菜单默认仅显示 10 年 用户必须单击去年才能添加更多年份 我们如何将初始范围设置为 100 年 以便用户默认看到一个大列表 function InitDatePickers datepicker datepick
  • GZipStream 进行 gzip 压缩,但解压缩文件最终会出现“数据意外结束”

    有谁知道为什么我在解压缩 gzip 文件时收到 数据意外结束 错误消息 为了验证字节数据没有损坏 我使用FooTest4 csv写入文件并能够成功打开该文件 两者都是 FooTest3 csv gz and FooTest2 csv gz解
  • 如何忽略 PHP 中未定义的变量错误[重复]

    这个问题在这里已经有答案了 我最近设置了一个新的网络服务器 但遇到了未定义的变量错误 如果我在没有初始化的情况下使用变量 则会出现错误 源代码没有改变 只有 LAMP 环境可以 你会如何解决这个问题 Thanks Well 您应该定义所有变
  • SVG viewBox 反转 Y 坐标

    我正在使用 SVG 在 HTML 页面中绘制不同的形状 这些不同的形状是从 Microsoft Sql Server 中的空间数据库的几何对象中检索的 我面临的问题是坐标系统 Svg 和 Microsoft Sql Server 不同 0
  • 调整highlight.sector() 宽度和位置 - R 中的和弦图(circlize 包)

    我需要一些帮助来调整突出显示的部分chordDiagram 来自 circlize 包 我正在处理渔业上岸数据 渔船从一个港口出发 母港PORT DE 并将他们的捕获物降落在另一个港口 登陆港PORT LA 我正在处理扇贝活重 吨 上岸量S