plot_grob <- p$gtable$grob[[1]]
xlab_grob <- p$gtable$grob[[2]]
ylab_grob <- p$gtable$grob[[3]]
legend_grob <- p$gtable$grob[[4]]
p$gtable$heights
p$gtable$widths
![在这里插入图片描述](https://img-blog.csdnimg.cn/ef920c55f5f8446e9183a7373a31bca4.jpeg#pic_center)
因为热图的原始布局是5行6列,因此对应5个行高,6个列宽。
现在,我们已经知道热图的一些原始布局信息了,如行高列宽、每个图形对象的位置等等。再回到一开始的需求,把图例放到左边,这就意味着4个图形对象的位置需要左右移动,图例(legend_grob)要向左移到可视化矩阵的位置,可视化矩阵(plot_grob)、列名称(xlab_grob)、行名称(ylab_grob)要向右移动。这也就要求,列宽需要在原始布局的列宽基础上,进行相应的调整,图例的列宽放到可视化矩阵前面,可视化矩阵及其他图形列宽按顺序后移,行高不变。
my_new_gt <- gtable(widths = unit.c(unit(5,"bigpts"),
unit(0,"bigpts"),
max(unit(1.1,"grobwidth",legend_grob),unit(12,"bigpts")+1.2*unit(1.1,"grobwidth",legend_grob)) + unit(1,"inches") ,
unit(495,"bigpts"),
unit(1,"grobwidth",ylab_grob) + unit(10,"bigpts"),
unit(0,"bigpts")
height = unit.c(unit(0,"npc"),
unit(5,"bigpts"),
unit(0,"bigpts"),
unit(330,"bigpts"),
unit(1,"grobheight",xlab_grob) + unit(10,"bigpts")
gtable <- gtable_add_grob(my_new_gt,legend_grob,t=3,l=3,b=5,r=3)
gtable <- gtable_add_grob(gtable,xlab_grob,5,4)
gtable <- gtable_add_grob(gtable,ylab_grob,4,5)
gtable <- gtable_add_grob(gtable,plot_grob,4,4)
到此,我们已经实现图例位置的调整了,效果如下:
![在这里插入图片描述](https://img-blog.csdnimg.cn/fe76a9b8fc1f46bfaca6bc980eba2962.png#pic_center)
然而,又出现了个问题,我们图例的文字部分显示不全,所以图例位置还得往下移动一小节。(对这个children,个人理解是一种子级的概念,如这个图例(legend_grob),由2个子级构成,一部分是图棒,另一部分是旁边的文字说明,所以向下移动意味着这两个子级的y轴都要向下移动)
legend_grob$children
legend_grob$children[[1]]$y <- legend_grob$children[[1]]$y - unit(0.05,"inches")
legend_grob$children[[2]]$y <- legend_grob$children[[2]]$y - unit(0.05,"inches")
到此为止,我们大功告成,请看最终效果:
![在这里插入图片描述](https://img-blog.csdnimg.cn/c1c7ba1f5b0047139934a628b0ef0a49.png#pic_center)
全部代码:
library(psych)
library(pheatmap)
library(ggplot2)
library(gtable)
library(grid)
mtcars
data_corr <- corr.test(mtcars, method="pearson", adjust="none")
data_r <- data_corr$r
data_p <- data_corr$p
getSig <- function(dc) {
sc <- ''
if (dc < 0.001) sc <- '***'
else if (dc < 0.01) sc <- '**'
else if (dc < 0.05) sc <- '*'
sig_mat <- matrix(sapply(data_p, getSig), nrow=nrow(data_p))
heatmap_pic_new <- pheatmap(data_r,cellwidth = 45,cellheight = 30,
cluster_row = F,cluster_col = F,angle_col=0,
display_numbers=sig_mat, fontsize_number=15)
p <- heatmap_pic_new
p$gtable
gtable_show_layout(p$gtable)
plot_grob <- p$gtable$grob[[1]]
xlab_grob <- p$gtable$grob[[2]]
ylab_grob <- p$gtable$grob[[3]]
legend_grob <- p$gtable$grob[[4]]
legend_grob$children
legend_grob$children[[1]]$y <- legend_grob$children[[1]]$y - unit(0.05,"inches")
legend_grob$children[[2]]$y <- legend_grob$children[[2]]$y - unit(0.05,"inches")
p$gtable$heights
p$gtable$widths
my_new_gt <- gtable(widths = unit.c(unit(5,"bigpts"),
unit(0,"bigpts"),
max(unit(1.1,"grobwidth",legend_grob),unit(12,"bigpts")+1.2*unit(1.1,"grobwidth",legend_grob)) + unit(1,"inches") ,
unit(495,"bigpts"),
unit(1,"grobwidth",ylab_grob) + unit(10,"bigpts"),
unit(0,"bigpts")
height = unit.c(unit(0,"npc"),
unit(5,"bigpts"),
unit(0,"bigpts"),
unit(330,"bigpts"),
unit(1,"grobheight",xlab_grob) + unit(10,"bigpts")
gtable <- gtable_add_grob(my_new_gt,legend_grob,t=3,l=3,b=5,r=3)
gtable <- gtable_add_grob(gtable,xlab_grob,5,4)
gtable <- gtable_add_grob(gtable,ylab_grob,4,5)
gtable <- gtable_add_grob(gtable,plot_grob,4,4)
png(filename = 'C:/Users/w/Desktop/mtcars_legend_1.png',width = 2500,height = 2000,res = 300)
grid.draw(gtable)
dev.off()
R语言绘制热图实践 (一)pheatmap前言pheatmap包pheatmap简介常用参数介绍使用安装绘制样本间相关系数图(简单使用)差异表达基因热图(进阶使用)pheatmap总结corrplot包参考资料
在生信分析中,我们常常需要计算一个样本的几次实验结果或者不同样本实验结果的相关系数(样本间相关系数)以判断几个数据集之间相关的程度。
在本篇中及之后的内容中,为了用R得到相关系数热图...
论文中展示基因表达量变化通常使用热图,今天分享一个快速绘制不同基因在各处理下表达量变化的方法,使用R语言中pheatmap包,它可以用于可视化数据集中的数值,以便更好地理解数据之间的关系和模式。通常绘制基因表达热图的目的是看不同基因和处理之间的变化关系,比如不同时间下基因的表达量变化,因此不需要对处理进行聚类,为了让图中横轴Type按照顺序排列,取消纵轴聚类,可以使用。直接运行pheatmap函数,输入数据矩阵,即可快速生成漂亮的热图,会自动对行和列进行聚类,这也是最简单的热图生成方法。
Pheatmap热图的绘制及如何调整图片
Pheatmap包是R语言绘制热图比较强大的软件包,当然现在也有很多资料介绍这个包的使用,但是今天我写的重点不是如何使用这个包绘制热图,而是如何绘制出更好看的热图。(我使用的矩阵是1663x594),下面的左图和右图来源于同一个数据。明显可以看出左图更加直观和美观。那么绘制左图的步骤主要分下面三步:
第一步,绘制成普通热图:
#读取数据
rt=r...
相关性热图的完美解决方案 – pheatmap包
install.packages('pheatmap')# 安装包,加载数据
library(pheatmap)
# 生成测试数据集
test = matrix(rnorm(200),20,10)
# 取出1-10行,13579列,全部加3
test[1: 10, seq(1,10,2)] = test[1:10, seq(1, 10,2)]+3
# 取出11-20行,246810列,全部加1
test[11: 20, seq(2,10,2)] = tes
1. read.table()
read.table(file, header = FALSE, sep = "", quote = "\"'",
dec = ".", numerals = c("allow.loss", "warn.loss", "no.loss"),
row.names, col.names, as.is = !stringsAsFactors,
na.strings = "NA", colClasses = NA, n
用R语言的pheatmap 包画热图可以给行或者列添加注释,比如添加个分组信息示例代码test = matrix(rnorm(200), 20, 10)test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2tes...
plt.legend(bbox_to_anchor=(0.5, -0.2),loc=8,ncol=10) # , borderaxespad=0
bbox_to_anchor=(0.5, -0.2) 这个自己调整几次就大概知道了,0.5与图例的水平方向有关,-0.2与图例的垂直方向有关
参考文献:Python_matplotlib画图时图例说明(legend)放到图像外侧