This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to determine width of heatmap

Hi there,

I have almost done with my heatmap but I cannot figure out how to modify the command in order to see the label. Please, see below:

## Import dataset
data <- read.table("Genes_with_function.txt", header = TRUE, sep = "\t", dec = ".")
mat_data <- data.matrix(data[,2:ncol(data)])

# transform to Z-scale
mat <- t(scale(t(data.matrix(mat_data))))

# set colour
myCol <- colorRampPalette(c("#f03b20", "#fff7bc", "#2c7fb8"))(100)
myBreaks <- seq(-2, 3, length.out=100)

# For ID
ID <- read.table("ID.txt", header = TRUE, sep = "\t", dec = ".")  ### a new .txt file with only the ID of the genes

# Do the plot
hmap <- pheatmap(mat,
             cluster_rows = TRUE,
             cluster_cols = TRUE,
             scale = 'none',
             breaks = myBreaks,
             col = myCol,
             show_colnames = TRUE,
             labels_row = ID$ID)

Then I get this heatmap: https://ibb.co/nzMMy5t

As you can see the names of the genes (TRINITY blahblah) is imposible to see. Is there any way of specify that?

By the way the ID file looks like this:

https://ibb.co/Scxr2S0

Thank you in advance

pheatmap label_row heatmap

You could modify the value of the width parameter that is passed to pheatmap(). Also, why not get rid of the 'TRINITY' prefix of the rownames? - it is not needed. You could also consider replacing the first _ with \n via:

sub('_', '\n', x)

Thank you much!

It worked with the commands cellwidth cellheight

But still the legend is over the text:

https://ibb.co/wr3jjrL

Any idea how to move to the left (for instance)?

I am not sure why it is doing that. I use cellwidth and it comfortably positions the legend after the labels, no matter how long are the labels.

Even without specifying width or cellwidth, my version of pheatmap correctly positions the colour bar:

data <- replicate(25, rnorm(40))
rownames(data) <- paste("Floccinaucinihilipilification_LooongGeneName", c(1:nrow(data)))
colnames(data) <- paste("Sample", c(1:ncol(data)))

pheatmap(data, 
    show_rownames=T,
    cluster_cols=T,
    cluster_rows=T,
    scale="row",
    clustering_distance_rows="euclidean",
    clustering_distance_cols="euclidean",
    clustering_method="complete",
    border_color=FALSE,
    cex=1.0)

d

Which version are you using? - I am using pheatmap_1.0.12 (R 3.5.3)

I am using the same version as you.

Maybe the problem is that I am using two different files and pheatmap does not recognize them?

I had assumed that s/he did try, but then I received a comment to my own comment (above). luzglongoria, can you confirm? - bioExplorer's solution should be good.

1 answer

Try this code and adjust the width parameter inside the png function, i.e. try changing width = 3100 to what is suitable to you until you see full name

## Import dataset
data <- read.table("Genes_with_function.txt", header = TRUE, sep = "\t", dec = ".")
mat_data <- data.matrix(data[,2:ncol(data)])

# transform to Z-scale
mat <- t(scale(t(data.matrix(mat_data))))

# set colour
myCol <- colorRampPalette(c("#f03b20", "#fff7bc", "#2c7fb8"))(100)
myBreaks <- seq(-2, 3, length.out=100)

# For ID
ID <- read.table("ID.txt", header = TRUE, sep = "\t", dec = ".")   

### a new .txt file with only the ID of the genes

png("heatmap.png", width=3100,height=2000, res=315, pointsize=12)

pheatmap(mat, color=colorRampPalette(c("red","yellow","grey","blue","green"))(60), cluster_cols=TRUE, cluster_rows = TRUE,              
scale = 'none', breaks = myBreaks, col = myCol, show_colnames = TRUE, labels_row = ID$ID)

dev.off()

Thank you for the comment. I used those commands and I got the same problem. Finally , I used the command cellwidth cellheight and it worked very well.

Log in to answer this question.