r - 调整绘图边距以显示图形图例
问题描述
如何调整绘图大小以使热图图例可见?
我试过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()
解决方案
如果您包含一个最小的可重现示例,那么帮助您解决问题会容易得多。请参阅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()
这能解决你的问题吗?
此外,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")
推荐阅读
- android - Gradle 挂在 :app:transformDexWithInstantRunSlicesApkForDebug
- docker - 大厅任务是否会覆盖 docker 图像入口点?
- django - Django modelform - 制作一个基于其他模型提供选择的模型表单
- jenkins - 无法安装插件
- javascript - Angular 6在客户端以pdf格式保存材料向导数据
- ios - Swift 在 UITableView 内的数组中获取值的第一个实例
- android - 这里不再允许使用元素 font-family :Android Studio
- php - Codeigniter - 尝试使用 postgresql 时出现问题
- java - Spring Batch Job 的连接重置问题
- google-analytics - 如何创建可以跟踪 API 调用的分析程序