R语言 调整图边距以显示图例

cunj1qz1  于 2023-11-14  发布在  其他
关注(0)|答案(1)|浏览(123)

如何调整图的大小以使热图图例可见?
我尝试了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()

字符串


的数据

nvbavucw

nvbavucw1#

为了复制你的问题,我下载了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()

字符串
x1c 0d1x的数据
这能解决你的问题吗
此外,R的一个伟大之处在于,人们创建包来使这些类型的任务更容易;一个例子是pheatmap package,它使聚类样本和注解样本组更加简单,我发现最终的图像可以比从头开始创建图更“漂亮”。

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


相关问题