【问题标题】:R heatmap combined with barplotR热图结合条形图
【发布时间】:2014-01-15 19:10:22
【问题描述】:

我正在尝试创建一个与条形图相结合的热图,以便在每一行的末尾都有一个长度与该行相关的条形图。这个想法是将以下两者合二为一:

library(gplots)
data(mtcars)
x  <- as.matrix(mtcars[,2:11])
hm<-heatmap(x)
barplot(mtcars[hm$rowInd,"mpg"],horiz=T,names.arg=row.names(mtcars)[hm$rowInd],las=2,cex.names=0.7,col="purple",2)

我的问题是如何在使行和条对齐的同时将两者结合起来? 谢谢。

【问题讨论】:

  • 你没有包括你的问题...
  • 我不知道你在树状图上的设置如何,但这里的问题是 heatmap() uses layout and draws the image in the lower right corner of a 2x2 layout. Consequentially, it can not be used in a multi column/row layout, i.e., when par(mfrow = *) or (mfcol = *) has been called. 我建议使用 ggplot2 进行热图和条形图。一个很好的例子可以在这里找到:learnr.wordpress.com/2010/01/26/ggplot2-quick-heatmap-plotting。如果您需要更多帮助,请告诉我。
  • @amzu 这听起来像是一个答案,不是吗?
  • 作为答案发布,谢谢@Amanda

标签: r bar-chart heatmap


【解决方案1】:

您不能合并这些图,因为(根据文档)heatmap() uses layout and draws the image in the lower right corner of a 2x2 layout. Consequentially, it can not be used in a multi column/row layout, i.e., when par(mfrow = *) or (mfcol = *) has been called.

最好的办法是使用 ggplot2 和 gridExtra 来组合图形。为此,需要使用 ggplot 创建热图和条形图。

你可以在ggplot2教程here找到热图。

一旦你有你的两个情节,使用以下命令将它们组合起来:

#Create the plots
g1 <- heatmap
g2 <- barplot

#Arrange them in a grid
gg1 <- ggplot_gtable(ggplot_build(g1))
gg2 <- ggplot_gtable(ggplot_build(g2))

grid.arrange(gg1, gg2, ncol=2)

【讨论】:

  • 谢谢。仅出于完整性考虑 - 可以使用 ggdendrogram 绘制树状图
猜你喜欢
  • 2012-05-22
  • 2012-08-19
  • 1970-01-01
  • 1970-01-01
  • 2023-02-07
  • 2015-10-15
  • 1970-01-01
  • 2021-01-17
  • 2023-03-22
相关资源
最近更新 更多