Circlize多层次数据的弦图
Circlize chord diagram with multiple levels of data
我发现自己有点卡住了,
我想通过 circlize 上的和弦图显示贩运物种区域之间的流动,但我无法弄清楚当第 1 列和第 2 列代表 "connection"、第 3 列是感兴趣的 "factor" 时如何绘制和第 4 列是值。
我在下面包含了一个数据样本(是的,我知道印度尼西亚是一个地区),正如您所看到的,每个物种都不是特定地区独有的。我想制作一个类似于下面包含的图,但将每个区域的 "countries" 替换为 "species"。这可能吗?
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
与我想要的类似的图表示例,请点击下面的link:
chord diagram
在 circlize 包中,ChordDiagram()
函数只允许一个 "from" 列,一个 "to" 列和一个可选的 "value" 列。但是,对于您的情况,实际上我们可以对原始数据框进行一些转换,将其修改为三列数据框。
在你的例子中,你想区分例如Acanthosaura_armata 在北美从 Acanthosaura_armata 在欧洲,一种解决方案是合并区域名称和物种名称,例如 Acanthosaura_armata|North_America
以形成唯一标识符。接下来我将演示如何通过circlize包来可视化这个数据集。
读入数据。注意我用下划线替换了 space。
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)
剧情是这样的:
按地区分组
代码类似,我就不解释了,直接附上post中的代码。情节看起来像这样:
## 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)
我发现自己有点卡住了, 我想通过 circlize 上的和弦图显示贩运物种区域之间的流动,但我无法弄清楚当第 1 列和第 2 列代表 "connection"、第 3 列是感兴趣的 "factor" 时如何绘制和第 4 列是值。 我在下面包含了一个数据样本(是的,我知道印度尼西亚是一个地区),正如您所看到的,每个物种都不是特定地区独有的。我想制作一个类似于下面包含的图,但将每个区域的 "countries" 替换为 "species"。这可能吗?
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
与我想要的类似的图表示例,请点击下面的link: chord diagram
在 circlize 包中,ChordDiagram()
函数只允许一个 "from" 列,一个 "to" 列和一个可选的 "value" 列。但是,对于您的情况,实际上我们可以对原始数据框进行一些转换,将其修改为三列数据框。
在你的例子中,你想区分例如Acanthosaura_armata 在北美从 Acanthosaura_armata 在欧洲,一种解决方案是合并区域名称和物种名称,例如 Acanthosaura_armata|North_America
以形成唯一标识符。接下来我将演示如何通过circlize包来可视化这个数据集。
读入数据。注意我用下划线替换了 space。
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)
剧情是这样的:
按地区分组
代码类似,我就不解释了,直接附上post中的代码。情节看起来像这样:
## 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)