Search code examples
rplotpheatmap

Column labels cropped when using pheatmap


I am plotting a heatmap using pheatmap (Documentation). I am plotting a matrix in a fairly straightforward way:

pheatmap(mat, annotation_col=df, labels_col=rld$Infection_Line, fontsize_row=5, fontsize_col=7) 

The bottom of my plot is getting cut off so that I can't see the column names at the bottom. It looks like this:

enter image description here

Please note that this is not heatmap.2.

I have tried the solutions at this question and at this question, as well as other things I've been able to find through google and in the documentation for this function.

I have tried to increase the margins using par() and oma(), as well as cexRow.

margins=(x,y); par(mar=c(1,2,3,4)); par(oma=c(1,2,3,4))  

have no effect on the plot.

I need to make it so that I can see these long column names without reducing my plot size. I just want to stretch the margin at the bottom down.


Solution

  • 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.