0

我发现自己有点卡住了,我想通过 circlize 上的和弦图显示被贩运物种的区域之间的流动,但是当第 1 列和第 2 列代表“连接”时,我无法弄清楚如何绘制,第 3 列是“因子”感兴趣,第 4 列是值。我在下面包含了一个数据样本(是的,我知道印度尼西亚是一个地区),因为您可以看到每个物种都不是特定地区独有的。我想制作一个类似于下面包含的情节,但将每个地区的“国家”替换为“物种”。这可能吗?

import_region    export_region  species                flow
North America    Europe         Acanthosaura armata     0.0104
Southeast Asia   Europe         Acanthosaura armata     0.0022
Indonesia        Europe         Acanthosaura armata     0.1971
Indonesia        Europe         Acrochordus granulatus  0.7846
Southeast Asia   Europe         Acrochordus granulatus  0.1101
Indonesia        Europe         Acrochordus javanicus   2.00E-04
Southeast Asia   Europe         Acrochordus javanicus   0.0015
Indonesia        North America  Acrochordus javanicus   0.0024
East Asia        Europe         Acrochordus javanicus   0.0028
Indonesia        Europe         Ahaetulla prasina       4.00E-04
Southeast Asia   Europe         Ahaetulla prasina       4.00E-04
Southeast Asia   East Asia      Amyda cartilaginea      0.0027
Indonesia        East Asia      Amyda cartilaginea      5.00E-04
Indonesia        Europe         Amyda cartilaginea      0.004
Indonesia        Southeast Asia Amyda cartilaginea      0.0334
Europe           North America  Amyda cartilaginea      4.00E-04
Indonesia        North America  Amyda cartilaginea      0.1291
Southeast Asia   Southeast Asia Amyda cartilaginea      0.0283
Indonesia        West Asia      Amyda cartilaginea      0.7614
South Asia       Europe         Amyda cartilaginea      2.8484
Australasia      Europe         Apodora papuana         0.0368
Indonesia        North America  Apodora papuana         0.324
Indonesia        Europe         Apodora papuana         0.0691
Europe           Europe         Apodora papuana         0.0106
Indonesia        East Asia      Apodora papuana         0.0129
Europe           North America  Apodora papuana         0.0034
East Asia        East Asia      Apodora papuana         2.00E-04
Indonesia        Southeast Asia Apodora papuana         0.0045
East Asia        North America  Apodora papuans         0.0042

图表示例与我想要的类似,请点击下面的链接: 和弦图

4

1 回答 1

5

在 circlize 包中,该ChordDiagram()函数只允许一个“from”列、一个“to”列和一个可选的“value”列。但是,在您的情况下,实际上我们可以对原始数据框进行一些转换,以将其修改为三列数据框。

在您的示例中,您想将北美的 Acanthosaura_armata 与欧洲的 Acanthosaura_armata 区分开来,一种解决方案是合并区域名称和物种名称Acanthosaura_armata|North_America以形成唯一标识符。接下来我将演示如何通过 circlize 包来可视化这个数据集。

读入数据。注意我用下划线替换了空格。

df = read.table(textConnection(
"import_region    export_region  species                flow
North_America    Europe         Acanthosaura_armata     0.0104
Southeast_Asia   Europe         Acanthosaura_armata     0.0022
Indonesia        Europe         Acanthosaura_armata     0.1971
Indonesia        Europe         Acrochordus_granulatus  0.7846
Southeast_Asia   Europe         Acrochordus_granulatus  0.1101
Indonesia        Europe         Acrochordus_javanicus   2.00E-04
Southeast_Asia   Europe         Acrochordus_javanicus   0.0015
Indonesia        North_America  Acrochordus_javanicus   0.0024
East_Asia        Europe         Acrochordus_javanicus   0.0028
Indonesia        Europe         Ahaetulla_prasina       4.00E-04
Southeast_Asia   Europe         Ahaetulla_prasina       4.00E-04
Southeast_Asia   East_Asia      Amyda_cartilaginea      0.0027
Indonesia        East_Asia      Amyda_cartilaginea      5.00E-04
Indonesia        Europe         Amyda_cartilaginea      0.004
Indonesia        Southeast_Asia Amyda_cartilaginea      0.0334
Europe           North_America  Amyda_cartilaginea      4.00E-04
Indonesia        North_America  Amyda_cartilaginea      0.1291
Southeast_Asia   Southeast_Asia Amyda_cartilaginea      0.0283
Indonesia        West_Asia      Amyda_cartilaginea      0.7614
South_Asia       Europe         Amyda_cartilaginea      2.8484
Australasia      Europe         Apodora_papuana         0.0368
Indonesia        North_America  Apodora_papuana         0.324
Indonesia        Europe         Apodora_papuana         0.0691
Europe           Europe         Apodora_papuana         0.0106
Indonesia        East_Asia      Apodora_papuana         0.0129
Europe           North_America  Apodora_papuana         0.0034
East_Asia        East_Asia      Apodora_papuana         2.00E-04
Indonesia        Southeast_Asia Apodora_papuana         0.0045
East_Asia        North_America  Apodora_papuans         0.0042"),
header = TRUE, stringsAsFactors = FALSE)

另外,我删除了一些值非常小的行。

df = df[df[[4]] > 0.01, ]

为物种和地区分配颜色。

library(circlize)
library(RColorBrewer)
all_species = unique(df[[3]])
color_species = structure(brewer.pal(length(all_species), "Set1"), names = all_species)
all_regions = unique(c(df[[1]], df[[2]]))
color_regions = structure(brewer.pal(length(all_regions), "Set2"), names = all_regions)

按物种分组

首先,我将演示如何按物种对和弦图进行分组。

如前所述,我们species|region用作唯一标识符。

df2 = data.frame(from = paste(df[[3]], df[[1]], sep = "|"),
                 to = paste(df[[3]], df[[2]], sep = "|"),
                 value = df[[4]], stringsAsFactors = FALSE)

接下来我们将所有扇区的顺序调整为先按物种排序,然后按地区排序。

combined = unique(data.frame(regions = c(df[[1]], df[[2]]), 
    species = c(df[[3]], df[[3]]), stringsAsFactors = FALSE))
combined = combined[order(combined$species, combined$regions), ]
order = paste(combined$species, combined$regions, sep = "|")

我们希望链接的颜色与 regoins 的颜色相同

grid.col = structure(color_regions[combined$regions], names = order)

由于弦图是按物种分组的,因此物种之间的差距应该大于每个物种内部的差距。

gap = rep(1, length(order))
gap[which(!duplicated(combined$species, fromLast = TRUE))] = 5

准备好所有设置后,我们现在可以制作和弦图:

在下面的代码中,我们设置preAllocateTracks以便之后添加代表物种的圆形线。

circos.par(gap.degree = gap)
chordDiagram(df2, order = order, annotationTrack = c("grid", "axis"),
    grid.col = grid.col, directional = TRUE,
    preAllocateTracks = list(
        track.height = 0.04,
        track.margin = c(0.05, 0)
    )
)

添加圆线以表示物种:

for(species in unique(combined$species)) {
    l = combined$species == species
    sn = paste(combined$species[l], combined$regions[l], sep = "|")
    highlight.sector(sn, track.index = 1, col = color_species[species], 
        text = species, niceFacing = TRUE)
}
circos.clear()

以及地区和物种的传说:

legend("bottomleft", pch = 15, col = color_regions, 
    legend = names(color_regions), cex = 0.6)
legend("bottomright", pch = 15, col = color_species, 
    legend = names(color_species), cex = 0.6)

情节如下所示:

group_by_species

按地区分组

代码类似,我就不解释了,只是把代码附在帖子里。情节如下所示:

group_by_regions

## group by regions
df2 = data.frame(from = paste(df[[1]], df[[3]], sep = "|"),
                 to = paste(df[[2]], df[[3]], sep = "|"),
                 value = df[[4]], stringsAsFactors = FALSE)

combined = unique(data.frame(regions = c(df[[1]], df[[2]]), 
    species = c(df[[3]], df[[3]]), stringsAsFactors = FALSE))
combined = combined[order(combined$regions, combined$species), ]
order = paste(combined$regions, combined$species, sep = "|")
grid.col = structure(color_species[combined$species], names = order)

gap = rep(1, length(order))
gap[which(!duplicated(combined$species, fromLast = TRUE))] = 5

circos.par(gap.degree = gap)
chordDiagram(df2, order = order, annotationTrack = c("grid", "axis"),
    grid.col = grid.col, directional = TRUE,
    preAllocateTracks = list(
        track.height = 0.04,
        track.margin = c(0.05, 0)
    )
)
for(region in unique(combined$regions)) {
    l = combined$regions == region
    sn = paste(combined$regions[l], combined$species[l], sep = "|")
    highlight.sector(sn, track.index = 1, col = color_regions[region], 
        text = region, niceFacing = TRUE)
}
circos.clear()

legend("bottomleft", pch = 15, col = color_regions, 
    legend = names(color_regions), cex = 0.6)
legend("bottomright", pch = 15, col = color_species, l
    egend = names(color_species), cex = 0.6)
于 2016-08-28T15:34:31.977 回答