我编写了一个生成弦图的简短函数。我希望该函数将图输出为一个对象,以便我在 PDF 中使用它,或保存到 RData 文件中。但是,我不希望...
我编写了一个生成和弦图的简短函数。我希望该函数将绘图输出为一个对象,以便我在 PDF 中使用它,或保存到 RData 文件中。但是,我不希望它输出到控制台。这是因为这是 rmarkdown 文件的一部分,我不希望它被绘图弄得乱七八糟。我似乎找不到一种在不破坏函数的情况下抑制绘图的方法 recordPlot()
。例如, pdf(NULL)
停止记录绘图。有人能想到解决办法吗?这是一个可行的示例:
comparison_table <- structure(c(0, 153, 429, 1, 29, 72, 11, 6, 81, 19, 13, 25, 119,
16, 36, 1, 40, 2, 160, 12, 2, 0, 20, 4, 1, 8, 4, 0, 1, 16, 6,
3, 3, 40, 1, 0, 0, 33, 2, 35, 0, 2, 0, 388, 26, 7, 27, 9, 11,
27, 36, 12, 6, 2, 179, 1, 4, 1, 23, 0, 34, 4, 4, 26, 4, 3, 0,
1, 2, 2, 0, 1, 0, 0, 0, 2, 2, 0, 0, 2, 0, 7, 0, 0, 0, 478, 62,
5, 23, 25, 17, 16, 77, 17, 8, 4, 192, 5, 10, 0, 28, 2, 117, 2,
2, 0, 27, 2, 0, 2, 1, 3, 0, 3, 1, 0, 0, 4, 0, 0, 0, 2, 0, 1,
0, 0, 0, 22, 1, 0, 10, 5, 6, 1, 22, 1, 4, 2, 10, 2, 0, 0, 3,
0, 31, 0, 2, 0, 17, 3, 0, 3, 3, 1, 2, 7, 4, 2, 0, 17, 0, 0, 0,
96, 0, 15, 1, 0, 0, 18, 9, 0, 3, 6, 4, 0, 19, 0, 3, 2, 13, 5,
4, 0, 2, 1, 28, 0, 1, 0, 13, 2, 0, 1, 3, 0, 3, 4, 4, 0, 0, 4,
0, 0, 0, 5, 0, 5, 0, 0, 0, 9, 2, 0, 4, 4, 0, 0, 8, 2, 0, 0, 12,
0, 0, 0, 1, 0, 5, 0, 1, 9, 297, 99, 1, 54, 145, 66, 11, 297,
21, 24, 15, 283, 117, 6, 17, 46, 18, 542, 2, 45, 1, 80, 10, 0,
2, 13, 14, 0, 32, 3, 0, 0, 27, 3, 0, 2, 2, 0, 21, 0, 1, 7, 312,
80, 4, 56, 73, 16, 28, 157, 17, 44, 9, 662, 22, 33, 29, 62, 16,
608, 4, 58, 1, 436, 68, 5, 55, 64, 27, 30, 183, 5, 27, 31, 340,
18, 5, 3, 50, 4, 245, 8, 16, 1, 7, 1, 0, 2, 0, 1, 3, 4, 0, 2,
0, 13, 0, 0, 0, 1, 1, 11, 0, 1, 0, 4, 10, 0, 3, 2, 0, 5, 6, 2,
2, 1, 2, 0, 1, 0, 8, 1, 22, 0, 2, 0, 6, 0, 0, 0, 1, 1, 2, 4,
0, 1, 1, 11, 2, 0, 1, 1, 1, 25, 0, 2, 0, 8, 7, 0, 2, 8, 4, 3,
31, 1, 2, 0, 36, 10, 0, 1, 1, 3, 169, 0, 9, 4, 108, 11, 11, 13,
51, 16, 7, 116, 2, 10, 4, 95, 14, 1, 36, 20, 8, 213, 0, 11, 0,
74, 12, 0, 8, 23, 15, 2, 121, 3, 1, 2, 29, 21, 2, 0, 5, 2, 192,
1, 3, 1, 22, 2, 0, 5, 11, 10, 1, 15, 0, 8, 3, 26, 0, 1, 2, 4,
1, 73, 1, 2, 0, 98, 2, 38, 8, 6, 1, 15, 22, 2, 5, 4, 150, 1,
1, 4, 12, 2, 23, 1, 1, 5, 138, 20, 3, 25, 65, 30, 12, 184, 6,
30, 11, 243, 62, 17, 17, 18, 19, 521, 1, 49, 9, 263, 21, 4, 37,
28, 10, 44, 80, 5, 14, 36, 597, 20, 6, 4, 38, 11, 220, 1, 27,
0, 8, 0, 0, 1, 5, 2, 0, 16, 1, 0, 0, 8, 2, 0, 0, 0, 1, 37, 0,
1, 0, 33, 1, 0, 1, 4, 1, 1, 15, 1, 0, 1, 30, 2, 0, 1, 2, 6, 29,
0, 2, 0, 24, 2, 0, 2, 7, 10, 0, 21, 1, 1, 1, 29, 1, 0, 10, 4,
0, 60, 0, 2), dim = c(21L, 28L), dimnames = list(c("royalblue1",
"brown1", "green1", "lightyellow1", "pink1", "red1", "purple1",
"tan1", "yellow1", "cyan1", "grey1", "midnightblue1", "blue1",
"magenta1", "grey601", "salmon1", "black1", "lightgreen1", "turquoise1",
"lightcyan1", "greenyellow1"), c("red2", "cyan2", "pink2", "darkturquoise2",
"black2", "white2", "grey602", "midnightblue2", "lightyellow2",
"orange2", "darkorange2", "blue2", "salmon2", "turquoise2", "brown2",
"darkgrey2", "darkred2", "darkgreen2", "tan2", "magenta2", "purple2",
"grey2", "greenyellow2", "green2", "yellow2", "royalblue2", "lightgreen2",
"lightcyan2")))
colour_vector <- c(royalblue1 = "royalblue", brown1 = "brown", green1 = "green",
lightyellow1 = "lightyellow", pink1 = "pink", red1 = "red", purple1 = "purple",
tan1 = "tan", yellow1 = "yellow", cyan1 = "cyan", grey1 = "grey",
midnightblue1 = "midnightblue", blue1 = "blue", magenta1 = "magenta",
grey601 = "grey60", salmon1 = "salmon", black1 = "black", lightgreen1 = "lightgreen",
turquoise1 = "turquoise", lightcyan1 = "lightcyan", greenyellow1 = "greenyellow",
red2 = "red", cyan2 = "cyan", pink2 = "pink", darkturquoise2 = "darkturquoise",
black2 = "black", white2 = "white", grey602 = "grey60", midnightblue2 = "midnightblue",
lightyellow2 = "lightyellow", orange2 = "orange", darkorange2 = "darkorange",
blue2 = "blue", salmon2 = "salmon", turquoise2 = "turquoise",
brown2 = "brown", darkgrey2 = "darkgrey", darkred2 = "darkred",
darkgreen2 = "darkgreen", tan2 = "tan", magenta2 = "magenta",
purple2 = "purple", grey2 = "grey", greenyellow2 = "greenyellow",
green2 = "green", yellow2 = "yellow", royalblue2 = "royalblue",
lightgreen2 = "lightgreen", lightcyan2 = "lightcyan")
dataset_pair <- c("rnaseq_20221003", "rnaseq_20240520")
chord_function <- function(comparison_table, colour_vector, dataset_pair) {
# Clear existing circos plot to reset parameters
circos.clear()
# Set gap degrees for sectors
circos.par(gap.after = c(rep(1, length(rownames(comparison_table)) - 1), 10, rep(1, length(colnames(comparison_table)) - 1), 10),
start.degree = -90, # Rotate the plot so that the first sector starts at the top
canvas.xlim = c(-1.5, 1.5), # Adjust x-axis limits for more space
canvas.ylim = c(-1.5, 1.5)) # Adjust y-axis limits for more space
# track.margin = c(0.01, 0.1), # Adjust track margins
# track.height = 0.05) # Adjust track height
# Create chord diagram
chordDiagram(comparison_table,
transparency = 0.5,
grid.col = colour_vector,
annotationTrack = "grid",
preAllocateTracks = list(track.height = 0.1))
# Add custom tracks and annotations
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(CELL_META$xcenter,
ylim[1] + cm_h(3), # Move the text further out
sector.name,
facing = "clockwise",
niceFacing = TRUE,
adj = c(0, 0.5))
circos.axis(h = "bottom",
labels.cex = 0.6,
sector.index = sector.name)
}, bg.border = NA)
# Add the title
title(paste(dataset_pair, collapse = " - "))
# Draw sectors
highlight.sector(rownames(comparison_table),
track.index = 1,
col = rgb(0, 0, 1, 0.3), # More opaque fill
border = NA) # Remove border
highlight.sector(colnames(comparison_table),
track.index = 1,
col = rgb(1, 0, 0, 0.3), # More opaque fill
border = NA) # Remove border
# Legend
legend("right",
legend = dataset_pair,
fill = c(rgb(0, 0, 1, 0.3), rgb(1, 0, 0, 0.3)),
border = NA,
bty = "n", # No box around the legend
cex = 1.2,
x.intersp = 0.25, # Reduce horizontal spacing between legend boxes and text
y.intersp = 0.75, # Reduce vertical spacing between legend lines
inset = c(-0.1, -0.1)) # Move the legend further from the plot
# Record
my_plot <- recordPlot()
# Output
return(my_plot)
}
plot_to_save <- chord_function(comparison_table = comparison,
colour_vector = my_colours,
dataset_pair = dataset_pair)
我尝试过在堆栈上查找和进行谷歌搜索,但解决方案似乎不起作用。