r pheatmap:在省略列名时如何为特定的行名进行着色?

发布于 2025-02-10 21:50:44 字数 1825 浏览 0 评论 0原文

在R中使用“ pheatmap”软件包,与Windows 10一起使用RSTUDIO,我可以按照示例(如下所示)只要热图显示行和列名称,就可以产生一个具有选择性着色的热图。但是,如果我尝试生成一个没有显示列名的选择性着色的热图,我会得到一个错误:

“ set.gpar中的错误(x $ gp,grob,grob = x):参数必须是'gpar'对象“

这是热图的借用示例:

library(pheatmap)
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 = "")

这是借用的借用示例,用于着色选择行名称:

library(grid)
library(gridExtra)
library(pheatmap)
hm <- pheatmap(test, silent = TRUE)
hm$gtable$grobs[[5]]$gp$col <- rep(c("black", "red"), each = 10)
leg <- legendGrob(c("Exp1", "Exp2"), nrow = 2, pch = 15, gp = gpar(fontsize = 10, col = c("black", "red")))
hm2 <- arrangeGrob(hm$gtable, leg, ncol = 2, widths = c(5,1))
grid.draw(hm2)

带有选定的行名称彩色红色的示例热图 但是,如果我尝试创建热图而不显示列的名称如下:

pheatmap(test, show_colnames=FALSE, silent=TRUE)
hm$gtable$grobs[[5]]$gp$col <- rep(c("black", "red"), each = 10)
leg <- legendGrob(c("Exp1", "Exp2"), nrow = 2, pch = 15, gp = gpar(fontsize = 10, col = c("black", "red")))
hm2 <- arrangeGrob(hm$gtable, leg, ncol = 2, widths = c(5,1))
grid.draw(hm2)

然后我会遇到一个错误:

set.gpar中的错误(x $ gp,grob,grob = x):参数必须是'gpar'对象,

  1. 为什么会出现此错误发生?
  2. 在不显示列名的同时,如何通过选择性着色来产生热图?

请注意,我更喜欢使用“ pheatmap”软件包。建议使用替代软件包(例如“ ComplexHeatMap”软件包)的答案,但由于各种原因,我希望尽可能使用“ pheatmap”软件包。谢谢。

Using the "pheatmap" package in R, with Rstudio for Windows 10, I can follow an example(as shown here) to produce a heatmap with selective coloring of row names as long as the heatmap displays both row and column names. However, if I try to generate a heatmap with selective coloring of row names without showing the column names, I get an error:

"Error in set.gpar(x$gp, grob = x) : argument must be a 'gpar' object"

Here is a borrowed example of a heatmap:

library(pheatmap)
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 = "")

And here is a borrowed example of a method for coloring selected row names:

library(grid)
library(gridExtra)
library(pheatmap)
hm <- pheatmap(test, silent = TRUE)
hm$gtable$grobs[[5]]$gp$col <- rep(c("black", "red"), each = 10)
leg <- legendGrob(c("Exp1", "Exp2"), nrow = 2, pch = 15, gp = gpar(fontsize = 10, col = c("black", "red")))
hm2 <- arrangeGrob(hm$gtable, leg, ncol = 2, widths = c(5,1))
grid.draw(hm2)

Sample heatmap with selected row names colored red
However, if I try to create the heatmap without showing the column names as follows:

pheatmap(test, show_colnames=FALSE, silent=TRUE)
hm$gtable$grobs[[5]]$gp$col <- rep(c("black", "red"), each = 10)
leg <- legendGrob(c("Exp1", "Exp2"), nrow = 2, pch = 15, gp = gpar(fontsize = 10, col = c("black", "red")))
hm2 <- arrangeGrob(hm$gtable, leg, ncol = 2, widths = c(5,1))
grid.draw(hm2)

Then I get an error:

Error in set.gpar(x$gp, grob = x) : argument must be a 'gpar' object

  1. Why does this error occur?
  2. How could the heatmap be produced with selective coloring of row names while not showing the column names?

Please note that I prefer to use the "pheatmap" package. Answers to similar questions have recommended using alternative packages such as the "ComplexHeatmap" package, but for various reasons, I prefer to use the "pheatmap" package if at all possible. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落日海湾 2025-02-17 21:50:45

为了获得错误,您的第一行在第三个代码段中应如下:

 hm <- pheatmap(test, show_colnames=FALSE, silent=TRUE)

简单解决方案:

 library(pheatmap)
 library(grid)

 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 = "")

 col_names = list(" ", " ", " ", " ", " ", " ", " ", " ", " ", " ") # use one space
 hm <- pheatmap(test, show_colnames=TRUE, silent=TRUE, labels_col = col_names)
 hm$gtable$grobs[[5]]$gp$col <- rep(c("black", "red"), each = 10)
 leg <- legendGrob(c("Exp1", "Exp2"), nrow = 2, pch = 15, gp = gpar(fontsize = 10, col = c("black", "red")))
 hm2 <- arrangeGrob(hm$gtable, leg, ncol = 2, widths = c(5,1))
 grid.draw(hm2)

To get the error your first line in the third code snippet should be as follows:

 hm <- pheatmap(test, show_colnames=FALSE, silent=TRUE)

The simple solution:

 library(pheatmap)
 library(grid)

 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 = "")

 col_names = list(" ", " ", " ", " ", " ", " ", " ", " ", " ", " ") # use one space
 hm <- pheatmap(test, show_colnames=TRUE, silent=TRUE, labels_col = col_names)
 hm$gtable$grobs[[5]]$gp$col <- rep(c("black", "red"), each = 10)
 leg <- legendGrob(c("Exp1", "Exp2"), nrow = 2, pch = 15, gp = gpar(fontsize = 10, col = c("black", "red")))
 hm2 <- arrangeGrob(hm$gtable, leg, ncol = 2, widths = c(5,1))
 grid.draw(hm2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文