在 R 中并排组合两个图(heatmap.2 图)

问题描述 投票:0回答:1

我有两个命令来生成两个不同的绘图:

plot(tree, show.tip.label = TRUE, direction = "rightwards", cex = 0.7)

heatmap.2(as.matrix(reordered_matrix), Rowv = FALSE, Colv = FALSE, dendrogram = "none", trace = "none", key = TRUE, symm = TRUE, col = bluered(100), margins = c(5, 5))

我想将这两个图合并起来,以便它们并排出现在同一个图表中,如下所示:enter image description here

这是我尝试过的:

dev.new(width = 12, height = 8)
x2 <- seq(1.9, 7.3, length.out=10)
x1 <- seq(0.2, 5.8, length.out=10)
y1 <- rnorm(10)
par(mfrow=c(1,2))
par(mar=c(5,4,4,0))
plot(tree, show.tip.label = TRUE, direction = "rightwards", cex = 0.7)
par(mar=c(5,0,4,2))    
heatmap.2(as.matrix(reordered_matrix),
          Rowv = FALSE, Colv = FALSE, dendrogram = "none", 
          trace = "none", key = TRUE, symm = TRUE, 
          col = bluered(100), margins = c(5, 5))

但是在绘制热图时,树消失了,问题出在 heatmap.2 函数上,我在论坛中没有找到类似的东西,因为如果我尝试在两个图中使用plot(),它会起作用,但没有到目前为止我找到的答案允许我这样做。

r plot heatmap
1个回答
0
投票

您应该先绘制热图,然后绘制树。图形参数 Fig 让我们可以精确地控制图形在绘图中的位置。我们需要以标准化形式提供坐标 c(x1, x2, y1, y2)。

plot.new()
par(fig = c(0.5, 1, 0, 1), new = TRUE)
gplots::heatmap.2(as.matrix(reordered_matrix), 
                  Rowv = FALSE, Colv = FALSE, dendrogram = "none", 
                  trace = "none", key = TRUE, symm = TRUE, 
                  col = gplots::bluered(100), margins = c(5, 5))

par(fig = c(0, 0.5, 0.2, 0.8), new = TRUE)
plot(tree, show.tip.label = TRUE, direction = "rightwards", cex = 0.7)

热图由“色键和直方图”和矩阵组成。如果您只想绘制矩阵,则必须先下载图形,然后裁剪它。在 image_crop() 中,几何定义了裁剪的大小和位置。格式为“宽度x高度+x偏移+y偏移”。

install.packages("magick")
library(magick)

png("my_plot.png", width = 800, height = 800)
gplots::heatmap.2(as.matrix(reordered_matrix), 
          Rowv = FALSE, Colv = FALSE, dendrogram = "none", 
          trace = "none", key = TRUE, symm = TRUE, 
          col = gplots::bluered(100), margins = c(5, 5))
dev.off()

img <- image_read("my_plot.png")
cropped_img <- image_crop(img, geometry = "800x800+200+200")

plot.new()
par(fig = c(0.5, 1, 0, 1), new = TRUE)
plot(cropped_img)

par(fig = c(0, 0.5, 0.2, 0.8), new = TRUE)
plot(tree, show.tip.label = TRUE, direction = "rightwards", cex = 0.7)
© www.soinside.com 2019 - 2024. All rights reserved.