如何在 R chordDiagram 中给标签上色
How to colour labels in R chordDiagram
从 几年前开始,我想使用 R 中的 circlize
包对 chordDiagram
中的段标签进行颜色编码。
?circos.text
中的文档告诉我,我应该使用图形 par
参数中的参数 col
来设置 this。但是,par(col)
不接受颜色向量。
有人可以请教如何做到这一点吗?非常感谢。
这里的示例代码使用 circlize
R 中的包。
library(circlize)
set.seed(999)
## generate example data
mat <- matrix(sample(18, 18), 3, 3)
rownames(mat) <- colnames(mat) <- paste0("A", 1:3)
df = data.frame(from = rep(rownames(mat), times = ncol(mat)),
to = rep(colnames(mat), each = nrow(mat)),
value = as.vector(mat),
stringsAsFactors = FALSE)
## set colours for segments
grid.col <- setNames(rainbow(nrow(mat)), rownames(mat))
# now, plot the image with rotated labels
chordDiagram(df, annotationTrack = "grid",
preAllocateTracks = 1,
grid.col = grid.col,
directional = 1,
direction.type = c("diffHeight", "arrows"),
link.arr.type = "big.arrow")
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")
circos.text(mean(xlim),
ylim[1] + .1,
sector.name,
facing = "clockwise",
niceFacing = TRUE,
adj = c(-0.5, 0.5))
circos.axis(h = "top",
labels.cex = 0.5,
major.tick.percentage = 0.2,
sector.index = sector.name,
track.index = 2)
}, bg.border = NA)
这产生了这个漂亮的情节
如何将标签 A1、A2 和 A3 更改为与其段相同的颜色? par(col = grid.col)
不起作用,因为它只需要矢量中的一种颜色。非常感谢。
> sessionInfo()
R version 4.0.0 (2020-04-24)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS High Sierra 10.13.6
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
attached base packages:
[1] stats4 parallel stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggplot2_3.3.1 reshape2_1.4.4 pRolocdata_1.26.0 pRoloc_1.29.0
[5] BiocParallel_1.22.0 MLInterfaces_1.68.0 cluster_2.1.0 annotate_1.66.0
[9] XML_3.99-0.3 AnnotationDbi_1.50.0 IRanges_2.22.2 MSnbase_2.14.2
[13] ProtGenerics_1.20.0 S4Vectors_0.26.1 mzR_2.22.0 Rcpp_1.0.4.6
[17] Biobase_2.48.0 BiocGenerics_0.34.0 dplyr_1.0.0 circlize_0.4.9
[21] migest_1.8.1
circos.trackPlotRegion
函数循环遍历其参数 x
,每次调用 panel.function
函数。效果是对每个调用 (col[1]
) 使用 col
的第一个值,而不是考虑参数 col
.
中的值向量
一个解决方案可能是为每个扇区调用 circos.trackPlotRegion
,如下所示:
# replace code after the call to `chordDiagram` with this
for (i in seq_len(nrow(mat))) { # use for loop to label each sector
myFactor <- rownames(mat)[i] # assuming this defines the sectors
myCol <- grid.col[i] # defined in the question
circos.trackPlotRegion(track.index = 1, factor = myFactor,
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")
circos.text(mean(xlim),
ylim[1] + .1,
sector.name,
col = myCol,
facing = "clockwise",
niceFacing = TRUE,
adj = c(-0.5, 0.5))
circos.axis(h = "top",
labels.cex = 0.5,
major.tick.percentage = 0.2,
sector.index = sector.name,
track.index = 2)
},
bg.border = NA)
}
或者,您可以提供颜色 grid.col[sector.name]
:
library(circlize)
set.seed(999)
## generate example data
mat <- matrix(sample(18, 18), 3, 3)
rownames(mat) <- colnames(mat) <- paste0("A", 1:3)
df = data.frame(from = rep(rownames(mat), times = ncol(mat)),
to = rep(colnames(mat), each = nrow(mat)),
value = as.vector(mat),
stringsAsFactors = FALSE)
## set colours for segments
grid.col <- setNames(rainbow(nrow(mat)), rownames(mat))
# now, plot the image with rotated labels
chordDiagram(df, annotationTrack = "grid",
preAllocateTracks = 1,
grid.col = grid.col,
directional = 1,
direction.type = c("diffHeight", "arrows"),
link.arr.type = "big.arrow")
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")
circos.text(mean(xlim),
ylim[1] + .1,
sector.name,
facing = "clockwise",
niceFacing = TRUE,
adj = c(-0.5, 0.5),
col=grid.col[sector.name])
circos.axis(h = "top",
labels.cex = 0.5,
major.tick.percentage = 0.2,
sector.index = sector.name,
track.index = 2)
}, bg.border = NA)
由 reprex package (v0.3.0)
于 2020-06-27 创建
从 circlize
包对 chordDiagram
中的段标签进行颜色编码。
?circos.text
中的文档告诉我,我应该使用图形 par
参数中的参数 col
来设置 this。但是,par(col)
不接受颜色向量。
有人可以请教如何做到这一点吗?非常感谢。
这里的示例代码使用 circlize
R 中的包。
library(circlize)
set.seed(999)
## generate example data
mat <- matrix(sample(18, 18), 3, 3)
rownames(mat) <- colnames(mat) <- paste0("A", 1:3)
df = data.frame(from = rep(rownames(mat), times = ncol(mat)),
to = rep(colnames(mat), each = nrow(mat)),
value = as.vector(mat),
stringsAsFactors = FALSE)
## set colours for segments
grid.col <- setNames(rainbow(nrow(mat)), rownames(mat))
# now, plot the image with rotated labels
chordDiagram(df, annotationTrack = "grid",
preAllocateTracks = 1,
grid.col = grid.col,
directional = 1,
direction.type = c("diffHeight", "arrows"),
link.arr.type = "big.arrow")
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")
circos.text(mean(xlim),
ylim[1] + .1,
sector.name,
facing = "clockwise",
niceFacing = TRUE,
adj = c(-0.5, 0.5))
circos.axis(h = "top",
labels.cex = 0.5,
major.tick.percentage = 0.2,
sector.index = sector.name,
track.index = 2)
}, bg.border = NA)
这产生了这个漂亮的情节
如何将标签 A1、A2 和 A3 更改为与其段相同的颜色? par(col = grid.col)
不起作用,因为它只需要矢量中的一种颜色。非常感谢。
> sessionInfo()
R version 4.0.0 (2020-04-24)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS High Sierra 10.13.6
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
attached base packages:
[1] stats4 parallel stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggplot2_3.3.1 reshape2_1.4.4 pRolocdata_1.26.0 pRoloc_1.29.0
[5] BiocParallel_1.22.0 MLInterfaces_1.68.0 cluster_2.1.0 annotate_1.66.0
[9] XML_3.99-0.3 AnnotationDbi_1.50.0 IRanges_2.22.2 MSnbase_2.14.2
[13] ProtGenerics_1.20.0 S4Vectors_0.26.1 mzR_2.22.0 Rcpp_1.0.4.6
[17] Biobase_2.48.0 BiocGenerics_0.34.0 dplyr_1.0.0 circlize_0.4.9
[21] migest_1.8.1
circos.trackPlotRegion
函数循环遍历其参数 x
,每次调用 panel.function
函数。效果是对每个调用 (col[1]
) 使用 col
的第一个值,而不是考虑参数 col
.
一个解决方案可能是为每个扇区调用 circos.trackPlotRegion
,如下所示:
# replace code after the call to `chordDiagram` with this
for (i in seq_len(nrow(mat))) { # use for loop to label each sector
myFactor <- rownames(mat)[i] # assuming this defines the sectors
myCol <- grid.col[i] # defined in the question
circos.trackPlotRegion(track.index = 1, factor = myFactor,
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")
circos.text(mean(xlim),
ylim[1] + .1,
sector.name,
col = myCol,
facing = "clockwise",
niceFacing = TRUE,
adj = c(-0.5, 0.5))
circos.axis(h = "top",
labels.cex = 0.5,
major.tick.percentage = 0.2,
sector.index = sector.name,
track.index = 2)
},
bg.border = NA)
}
或者,您可以提供颜色 grid.col[sector.name]
:
library(circlize)
set.seed(999)
## generate example data
mat <- matrix(sample(18, 18), 3, 3)
rownames(mat) <- colnames(mat) <- paste0("A", 1:3)
df = data.frame(from = rep(rownames(mat), times = ncol(mat)),
to = rep(colnames(mat), each = nrow(mat)),
value = as.vector(mat),
stringsAsFactors = FALSE)
## set colours for segments
grid.col <- setNames(rainbow(nrow(mat)), rownames(mat))
# now, plot the image with rotated labels
chordDiagram(df, annotationTrack = "grid",
preAllocateTracks = 1,
grid.col = grid.col,
directional = 1,
direction.type = c("diffHeight", "arrows"),
link.arr.type = "big.arrow")
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")
circos.text(mean(xlim),
ylim[1] + .1,
sector.name,
facing = "clockwise",
niceFacing = TRUE,
adj = c(-0.5, 0.5),
col=grid.col[sector.name])
circos.axis(h = "top",
labels.cex = 0.5,
major.tick.percentage = 0.2,
sector.index = sector.name,
track.index = 2)
}, bg.border = NA)
由 reprex package (v0.3.0)
于 2020-06-27 创建