melil
melil

Reputation: 81

Adjust plot margins to show figure legend

How do I adjust my plot size to make the heatmap legend visible?
I tried par(oma=c(0,0,1,0)+1, mar=c(0,0,0,0)+1) but it completely truncated my plot.

# 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()

enter image description here

Upvotes: 0

Views: 253

Answers (1)

jared_mamrot
jared_mamrot

Reputation: 26484

In order to replicate your issue, I downloaded a subset of the GEO dataset and used the mean affy intensities to create an approximation of your heatmap:

# 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()

example_3.png

Does this solve your problem?

Also, one of the great things about R is that people create packages to make these types of tasks easier; one example is the pheatmap package which makes clustering samples and annotating sample groups a lot more straightforward and I've found that the final image can be 'nicer' than creating the plot from scratch. E.g.

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

example_2.png

Upvotes: 1

Related Questions