R 中两端重叠的和弦图()

发布于 2025-01-14 18:05:38 字数 691 浏览 2 评论 0原文

我正在尝试使用 R 中的 circlize 包中的 chordDiagram() 函数在 R 中生成和弦图的修改版本。

假设我们有这个玩具示例(至于包的文档):

require(circlize)
df = expand.grid(letters[1:3], LETTERS[1:4])
df$value = 1
df$value2 = 3 
chordDiagram(df[, 1:3])

输出将非常简单:

在此处输入图像描述

现在,问题是:让我们假设我需要使两个“端”重叠来表示交叉点,例如,从 b-->A 出发的色带和从 a-->A 出发的色带,因此它们在同一扇区中结束,彼此“合并”(如下图所示)。 输入图片此处描述

这可能吗?我在文档中没有找到任何示例,也无法通过调整选项来执行此任务。

希望我说清楚了。感谢您的任何尝试。

I am trying to produce a modified version of a Chord Diagram in R using the chordDiagram() function in the circlize package in R.

Suppose we have this toy example (as for the documentation of the package):

require(circlize)
df = expand.grid(letters[1:3], LETTERS[1:4])
df$value = 1
df$value2 = 3 
chordDiagram(df[, 1:3])

The output will be pretty straightforward:

enter image description here

Now, the problem is: let's suppose that I need to make two "ends" overlap to represent an intersection, for example, the ribbon going from b-->A and the ribbon going from a-->A, so that they end in the same sector "merging" each other (such as in the figure below).
enter image description here

Is that possible? I didn't find any example in the documentation nor I was able to perform this task by tweaking the options.

Hope that I made myself clear. Thank you for any try.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

永言不败 2025-01-21 18:05:38

如果我理解正确的话,你想在图中制作重叠的部分。例如 S3 -> E2S2 -> E2。您创建一个具有格式的矩阵,以便您在图中获得重叠的路径。我将用以下代码为您提供一个示例:

首先创建一个在 S 和 E 之间具有不同值的连接的矩阵:

library(circlize)
set.seed(999)
mat = matrix(sample(18, 18), 3, 6) 
rownames(mat) = paste0("S", 1:3)
colnames(mat) = paste0("E", 1:6)
mat

   E1 E2 E3 E4 E5 E6
S1  4 14 13 17  5  2
S2  7  1  6  8 12 15
S3  9 10  3 16 11 18

接下来创建一个数据框,其格式为列的格式,其中字符从到另一个字符,并带有值以检查模式更多轻松:

df = data.frame(from = rep(rownames(mat), times = ncol(mat)),
                to = rep(colnames(mat), each = nrow(mat)),
                value = as.vector(mat),
                stringsAsFactors = FALSE)

   from to value
1    S1 E1     4
2    S2 E1     7
3    S3 E1     9
4    S1 E2    14
5    S2 E2     1
6    S3 E2    10
7    S1 E3    13
8    S2 E3     6
9    S3 E3     3
10   S1 E4    17
11   S2 E4     8
12   S3 E4    16
13   S1 E5     5
14   S2 E5    12
15   S3 E5    11
16   S1 E6     2
17   S2 E6    15
18   S3 E6    18

使用 mat 绘制输出:

chordDiagram(mat)

在此处输入图像描述

如您所见,S3 -> 的路径存在一些重叠。 E2S2 -> E2。

If I understand you correctly, you want to make overlapping parts in the diagram. So for example S3 -> E2 and S2 -> E2. You create a matrix which has a format so that you get overlapping paths in the diagram. I will give you an example with the following code:

First create a matrix with connection between S and E with different values:

library(circlize)
set.seed(999)
mat = matrix(sample(18, 18), 3, 6) 
rownames(mat) = paste0("S", 1:3)
colnames(mat) = paste0("E", 1:6)
mat

   E1 E2 E3 E4 E5 E6
S1  4 14 13 17  5  2
S2  7  1  6  8 12 15
S3  9 10  3 16 11 18

Next create a dataframe which has a format of columns with character from to another character with values to check what the patterns are more easily:

df = data.frame(from = rep(rownames(mat), times = ncol(mat)),
                to = rep(colnames(mat), each = nrow(mat)),
                value = as.vector(mat),
                stringsAsFactors = FALSE)

   from to value
1    S1 E1     4
2    S2 E1     7
3    S3 E1     9
4    S1 E2    14
5    S2 E2     1
6    S3 E2    10
7    S1 E3    13
8    S2 E3     6
9    S3 E3     3
10   S1 E4    17
11   S2 E4     8
12   S3 E4    16
13   S1 E5     5
14   S2 E5    12
15   S3 E5    11
16   S1 E6     2
17   S2 E6    15
18   S3 E6    18

Plot the output using mat:

chordDiagram(mat)

enter image description here

As you can see, there is some overlap in the paths of S3 -> E2 and S2 -> E2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文