Leon Smith
Leon Smith

Reputation: 117

How to fix heatmap legend out of margin?

I want to increase the font size of the heatmap, but I found when the lengend name is long and legend group name is short, the legend will easily out of margin. Here is the example:

test = matrix(rnorm(200), 20, 10)
test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3
test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2
test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4
colnames(test) = paste("Test", 1:10, sep = "")
rownames(test) = paste("Gene", 1:20, sep = "")

annotation_col = data.frame(
  C = factor(rep(c("longCT1", "longCT2"), 5))
)
rownames(annotation_col) = paste("Test", 1:10, sep = "")
pheatmap::pheatmap(test, 
         annotation_col = annotation_col,
         cluster_cols = F,
         scale = "row", 
         fontsize = 20)

You can see the legend name C is short and lengned group name longCTx is longer, and when set a big font size, it will out of margin:

enter image description here

So in this case, how to fix it when I want to increase the font size of the heatmap?

Upvotes: 0

Views: 625

Answers (1)

Marco Sandri
Marco Sandri

Reputation: 24272

A simple solution is to add some blanks at the end of the group names. See below:

# Add blanks to groups names
annotation_col = data.frame(
  C = factor(rep(c("longCT1    ", "longCT2    "), 5))
)
rownames(annotation_col) = paste("Test", 1:10, sep = "")
pheatmap::pheatmap(test, 
         annotation_col = annotation_col,
         cluster_cols = F,
         scale = "row", 
         fontsize = 20)

enter image description here

Upvotes: 1

Related Questions