I am plotting a heatmap using pheatmap
(Documentation). I am plotting a matrix in a fairly straightforward way:
pheatmap(mat, a
I would like to elaborate a bit on rmf's answer.
The key issue is to fiddle with the cellwidth
and cellheight
as well as width
and height
. The former changes how many pixels are taken up by one cell, the latter changes how big the output image will be. So, if your plot clips past the edge of the image, you can either decrease the cell size or increase the image size.
Note that this only affects the output file, not the R plotting area you see when R is running.
Example:
library(pheatmap)
set.seed(1)
data <- as.data.frame(matrix(rnorm(600), ncol=20))
rownames(data) <- paste(rownames(data), "looooong text")
then...
# Doesn't fit on image
pheatmap(data, filename = "/tmp/example_1.png",
cellwidth = 20, cellheight = 20,
width = 7, height=9.1)
# Change cell sizes, keep width/height fixed
pheatmap(data, filename = "/tmp/example_2.png",
cellwidth = 10, cellheight = 10, # <--- changed here
width = 7, height=9.1)
# Change width/height, keep cell sizes fixed
pheatmap(data, filename = "/tmp/example_3.png",
cellwidth = 20, cellheight = 20,
width = 9, height=10) # <--- changed here
pheatmap
uses grid graphics, so base graphics functions like par()
is not going have an effect. I find that adjusting arguments cellheight
and cellwidth
manually can help to adjust the overall size of the heatmap on the page. Or in some way adjusting the margin.
library(pheatmap)
dfr <- as.data.frame(t(data.frame(x=runif(10),y=runif(10),z=runif(10))))
md <- data.frame(cat1=sample(x=letters[1:4],10,replace=T),cat2=sample(x=letters[6:7],10,replace=T))
rownames(md) <- colnames(dfr)
pheatmap(dist(as.data.frame(t(dfr))),annotation_col=md,annotation_row=md)
pheatmap(dist(as.data.frame(t(dfr))),annotation_col=md,annotation_row=md,
cellheight=15,cellwidth=15)
I figured this out, hopefully if anyone has this problem in the future it will help.
This happens when you are using the labels_col= argument of pheatmap. In my scenario, which was a RNA-seq project using DESeq2, there was a targets file identifying the samples (columns), but for my column labels I was using a different column so the labels were more understandable, so I used
labels_col=myThing$ThisOtherColumn
The other column, while actually a string containing characters and numbers, for some reason was being read as an integer vector. So the solution was to do
as.character(myThing$ThisOtherColumn)
As long as you give labels_col a character vector it will adjust the columns automatically.