#Edited to add in library names
library(gtable)
library(grid)
library(pheatmap)
#Starting with data and generating initial plot
test <- matrix(rexp(200, rate=.1), ncol=20)
colnames(test) = paste("Room", 1:20, sep = "")
rownames(test) = paste("Building", 1:10, sep = "")
p<-pheatmap(test, legend = TRUE, cluster_rows = FALSE, cluster_cols = FALSE)
#Get grobs we want - will use these to create own plot later
p$gtable$grob
## [[1]]
## gTree[GRID.gTree.6]
##
## [[2]]
## text[GRID.text.7]
##
## [[3]]
## text[GRID.text.8]
##
## [[4]]
## gTree[GRID.gTree.11]
plot.grob <- p$gtable$grob[[1]]
xlab.grob <- p$gtable$grob[[2]]
ylab.grob <- p$gtable$grob[[3]]
legend.grob <- p$gtable$grob[[4]]
#Shift both down by 1 inch
legend.grob$children[[1]]$y <- legend.grob$children[[1]]$y - unit(0.85,"inches")
legend.grob$children[[2]]$y <- legend.grob$children[[2]]$y - unit(0.85,"inches")
legend.grob$children[[1]]$x <- legend.grob$children[[1]]$x + unit(0.4,"inches")
legend.grob$children[[2]]$x <- legend.grob$children[[2]]$x + unit(0.4,"inches")
#New legend label grob
leg_label <- textGrob("Temperature [°C]",x=0,y=0.9,hjust=0,vjust=0,gp=gpar(fontsize=10,fontface="bold"))
#Add label to legend grob
legend.grob2 <- addGrob(legend.grob,leg_label)
grid.draw(legend.grob2)
p$gtable
## TableGrob (5 x 6) "layout": 4 grobs
## z cells name grob
## 1 1 (4-4,3-3) matrix gTree[GRID.gTree.6]
## 2 2 (5-5,3-3) col_names text[GRID.text.7]
## 3 3 (4-4,4-4) row_names text[GRID.text.8]
## 4 4 (3-5,5-5) legend gTree[GRID.gTree.11]
p$gtable$heights
## [1] 0npc
## [2] 5bigpts
## [3] 0bigpts
## [4] sum(1npc, -1grobheight, -10bigpts, -5bigpts, 0bigpts)
## [5] sum(1grobheight, 10bigpts)
p$gtable$widths
## [1] 5bigpts
## [2] 0bigpts
## [3] sum(1npc, -1grobwidth, -10bigpts, -1*max(1.1grobwidth, sum(12bigpts, 1.32grobwidth)), -5bigpts, 0bigpts, 0bigpts)
## [4] sum(1grobwidth, 10bigpts)
## [5] max(1.1grobwidth, sum(12bigpts, 1.32grobwidth))
## [6] 0bigpts
#gtable_show_layout(p$gtable)
my_new_gt <- gtable(widths= unit.c(unit(0,"bigpts") + unit(5,"bigpts"),
unit(0,"bigpts"),
unit(1,"npc") - unit(1,"grobwidth",plot.grob) + unit(10,"bigpts") - max(unit(1.1,"grobwidth",plot.grob), (unit(12,"bigpts")+1.2*unit(1.1,"grobwidth",plot.grob))) + unit(5,"bigpts") - unit(3,"inches"),
unit(1,"grobwidth",ylab.grob) + unit(10,"bigpts"),
max(unit(1,"grobwidth",legend.grob2),unit(12,"bigpts")+1.2*unit(1.1,"grobwidth",legend.grob2)) + unit(1,"inches") ,
max(unit(0,"bigpts"),unit(0,"bigpts"))),
height = unit.c(unit(0,"npc"),
unit(5,"bigpts"),
unit(0,"bigpts"),
unit(1,"npc") - unit(1,"grobheight",xlab.grob) + unit(15,"bigpts") - unit(0.2,"inches"),
unit(1,"grobheight",xlab.grob) + unit(15,"bigpts")
))
#Adding each grob to the appropriate spot
gtable <- gtable_add_grob(my_new_gt,plot.grob,4,3)
gtable <- gtable_add_grob(gtable,xlab.grob,5,3)
gtable <- gtable_add_grob(gtable,ylab.grob,4,4)
gtable <- gtable_add_grob(gtable,legend.grob2,4,5)
grid.draw(gtable)

##########ref https://stackoverflow.com/questions/36852101/r-legend-title-or-units-when-using-pheatmap