gpt4 book ai didi

R用数据框的circlize制作圆/弦图

转载 作者:行者123 更新时间:2023-12-04 03:15:30 32 4
gpt4 key购买 nike

我想使用 circlize 包制作和弦图。我有一个包含四列汽车的数据框。前 2 列包含有关所拥有的汽车品牌和型号的信息,接下来的两列包含受访者迁移到的品牌和型号。

这是数据框的一个简单示例:

   Brand_from model_from Brand_to Model_to
1: VOLVO s80 BMW 5series
2: BMW 3series BMW 3series
3: VOLVO s60 VOLVO s60
4: VOLVO s60 VOLVO s80
5: BMW 3series AUDI s4
6: AUDI a4 BMW 3series
7: AUDI a5 AUDI a5

能够将其制作成和弦图会很棒。我在帮助中找到了一个有效的示例,但我无法将我的数据转换为正确的格式以制作绘图。
此代码来自 circlize 包中的帮助。这会产生一层,我想我需要两层,品牌和型号。
mat = matrix(1:18, 3, 6)
rownames(mat) = paste0("S", 1:3)
colnames(mat) = paste0("E", 1:6)

rn = rownames(mat)
cn = colnames(mat)
factors = c(rn, cn)
factors = factor(factors, levels = factors)
col_sum = apply(mat, 2, sum)
row_sum = apply(mat, 1, sum)
xlim = cbind(rep(0, length(factors)), c(row_sum, col_sum))

par(mar = c(1, 1, 1, 1))
circos.par(cell.padding = c(0, 0, 0, 0))
circos.initialize(factors = factors, xlim = xlim)
circos.trackPlotRegion(factors = factors, ylim = c(0, 1), bg.border = NA,
bg.col = c("red", "green", "blue", rep("grey", 6)), track.height = 0.05,
panel.fun = function(x, y) {
sector.name = get.cell.meta.data("sector.index")
xlim = get.cell.meta.data("xlim")
circos.text(mean(xlim), 1.5, sector.name, adj = c(0.5, 0))
})

col = c("#FF000020", "#00FF0020", "#0000FF20")
for(i in seq_len(nrow(mat))) {
for(j in seq_len(ncol(mat))) {
circos.link(rn[i], c(sum(mat[i, seq_len(j-1)]), sum(mat[i, seq_len(j)])),
cn[j], c(sum(mat[seq_len(i-1), j]), sum(mat[seq_len(i), j])),
col = col[i], border = "white")
}
}
circos.clear()

此代码生成以下图:

enter image description here

理想的结果就像这个例子,但我想要汽车品牌而不是大陆,而在内圈是属于该品牌的汽车模型
enter image description here

最佳答案

当我稍微更新了这个包时,现在有一种更简单的方法来做到这一点。如果有人对此感兴趣,我会在这里给出另一个答案。

的最新几个版本中圈子 , chordDiagram()接受邻接矩阵和邻接列表作为输入,这意味着,现在您可以提供一个包含与函数的成对关系的数据框。还有一个highlight.sector()可同时高亮或标记多个扇区的功能。

我将实现我之前展示的情节,但代码更短:

df = read.table(textConnection("
brand_from model_from brand_to model_to
VOLVO s80 BMW 5series
BMW 3series BMW 3series
VOLVO s60 VOLVO s60
VOLVO s60 VOLVO s80
BMW 3series AUDI s4
AUDI a4 BMW 3series
AUDI a5 AUDI a5
"), header = TRUE, stringsAsFactors = FALSE)

brand = c(structure(df$brand_from, names=df$model_from),
structure(df$brand_to,names= df$model_to))
brand = brand[!duplicated(names(brand))]
brand = brand[order(brand, names(brand))]
brand_color = structure(2:4, names = unique(brand))
model_color = structure(2:8, names = names(brand))
brand 的值, brand_colormodel_color是:
> brand
a4 a5 s4 3series 5series s60 s80
"AUDI" "AUDI" "AUDI" "BMW" "BMW" "VOLVO" "VOLVO"
> brand_color
AUDI BMW VOLVO
2 3 4
> model_color
a4 a5 s4 3series 5series s60 s80
2 3 4 5 6 7 8

这一次,我们只添加了一个额外的轨道,用于放置线路和品牌名称。而且您还可以发现输入变量实际上是一个数据框( df[, c(2, 4)] )。
library(circlize)
gap.degree = do.call("c", lapply(table(brand), function(i) c(rep(2, i-1), 8)))
circos.par(gap.degree = gap.degree)

chordDiagram(df[, c(2, 4)], order = names(brand), grid.col = model_color,
directional = 1, annotationTrack = "grid", preAllocateTracks = list(
list(track.height = 0.02))
)

和之前一样,手动添加模型名称:
circos.trackPlotRegion(track.index = 2, panel.fun = function(x, y) {
xlim = get.cell.meta.data("xlim")
ylim = get.cell.meta.data("ylim")
sector.index = get.cell.meta.data("sector.index")
circos.text(mean(xlim), mean(ylim), sector.index, col = "white", cex = 0.6, facing = "inside", niceFacing = TRUE)
}, bg.border = NA)

最后,我们通过 highlight.sector() 添加线条和品牌名称。功能。这里的值 sector.index可以是长度大于 1 的向量,并且线(或细矩形)将覆盖所有指定的扇区。扇区中间会加一个标签,部首位置由 text.vjust控制。选项。
for(b in unique(brand)) {
model = names(brand[brand == b])
highlight.sector(sector.index = model, track.index = 1, col = brand_color[b],
text = b, text.vjust = -1, niceFacing = TRUE)
}

circos.clear()

enter image description here

关于R用数据框的circlize制作圆/弦图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27500041/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com