首页 > 解决方案 > 调整绘图边距以显示图形图例

问题描述

如何调整绘图大小以使热图图例可见?
我试过par(oma=c(0,0,1,0)+1, mar=c(0,0,0,0)+1)了,但它完全截断了我的情节。

# Correlation Matrix
dat.cor  <- cor(samp.matrix, method="pearson", use="pairwise.complete.obs")
cx <- redgreen(50)

# Correlation plot - heatmap
png("Heatmap_cor.matrix.png")   
#par(oma=c(0,0,1,0), mar=c(0,0,0,0))
leg <- seq(min(dat.cor, na.rm=T), max(dat.cor, na.rm=T), length=10)
image(dat.cor, main="Correlation between Glioma vs Non-Tumor\n Gene Expression", col=cx, axes=F)
axis(1,at=seq(0,1,length=ncol(dat.cor)),label=dimnames(dat.cor)[[2]], cex.axis=0.9,las=2)
axis(2,at=seq(0,1,length=ncol(dat.cor)),label=dimnames(dat.cor)[[2]], cex.axis=0.9,las=2)
dev.off()

在此处输入图像描述

标签: rplotheatmapcorrelationscatter-plot

解决方案


如果您包含一个最小的可重现示例,那么帮助您解决问题会容易得多。请参阅https://stackoverflow.com/help/how-to-ask以获取有关改进问题并提高获得答案的机会的提示。

为了复制您的问题,我下载了 GEO 数据集的子集,并使用平均 affy 强度来创建热图的近似值:

# Load libraries
library(tidyverse)
#BiocManager::install("affyio")
library(affyio)

# GSE data downloaded from https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GSE4290
list_of_files <- fs::dir_ls("~/Desktop/GSE4290_RAW/")

# Load the CEL files
CEL_list <- list()
for (f in seq_along(list_of_files)) {
  CEL_list[[f]] <- read.celfile(list_of_files[[f]],
                                intensity.means.only = TRUE)
}

# Rename each element of the list with the corresponding sample name
names(CEL_list) <- gsub(x = basename(list_of_files),
                        pattern = ".CEL.gz",
                        replacement = "")

# Create a matrix of the mean intensities for all genes
samp.matrix <- map(CEL_list, pluck, "INTENSITY", "MEAN") %>%
  bind_cols() %>% 
  as.matrix()

# Calculate correlations between samples
dat.cor <- cor(samp.matrix, method = "pearson",
               use = "pairwise.complete.obs")

# Specify a colour palette (green/red is NOT colourblind friendly)
cx <- colorRampPalette(viridis::inferno(50))(50)

# Plot the heatmap
png("Heatmap_cor.matrix.png")
par(oma=c(0,0,1,0), mar=c(6,6,4,7), par(xpd = TRUE))
leg <- seq(from = 0.1, to = 1, length.out = 10)
image(dat.cor, main="Correlation between Glioma vs Non-Tumor\n Gene Expression", col=cx, axes=F)
axis(1,at=seq(0,1,length=ncol(dat.cor)),label=dimnames(dat.cor)[[2]], cex.axis=0.9,las=2)
axis(2,at=seq(0,1,length=ncol(dat.cor)),label=dimnames(dat.cor)[[2]], cex.axis=0.9,las=2)
legend(1.1, 1.1, title = "Correlation", legend = leg,
       fill = colorRampPalette(viridis::inferno(50))(10))
dev.off()

示例_3.png

这能解决你的问题吗?

此外,R 的一大优点是人们创建包以使这些类型的任务更容易。一个例子是pheatmap 包,它使聚类样本和注释样本组更加直接,我发现最终图像可能比从头开始创建图“更好”。例如

library(pheatmap)
pheatmap(mat = dat.cor, color = cx, border_color = "white", legend = TRUE,
         main = "Correlation between Glioma vs Non-Tumor\n Gene Expression")

示例_2.png


推荐阅读