R语言 当排列为单个图时,图不显示?

pjngdqdw  于 2023-01-06  发布在  其他
关注(0)|答案(1)|浏览(191)

我创建了一个热图和一个PCA图,并试图将它们合并为一个图。但是它们没有显示为一个图。

library(factoextra)
library(FactoMineR)
library(pheatmap)
library(RColorBrewer)
library(ggpubr)

# make test matrix
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 = "")

# define the annotation
annotation_row = data.frame(GeneClass = factor(rep(c("Path1", "Path2", "Path3"), c(10, 4, 6))),
                  AdditionalAnnotation = c(rep("random1", 10), rep("random2", 10)))
rownames(annotation_row) = paste("Gene", 1:20, sep = "")

a=pheatmap(test, annotation_row = annotation_row)

# creating pca

# Compute PCA with ncp = 3
res.pca <- PCA(test, ncp = 3, graph = FALSE)
# Compute hierarchical clustering on principal components
res.hcpc <- HCPC(res.pca, graph = FALSE)

# Principal components + tree
b=plot(res.hcpc, choice = "3D.map")

#arranging in a single plot
ggarrange(a$gtable, b, labels = c("A", "B"))

输出无pca:

t3irkdon

t3irkdon1#

plot(或plot.HCPC)返回NULL,因此bNULL
?ggarrange开始,它期望一个图表列表被安排到网格中,图表可以是ggplot2图表对象,也可以是任意的gtable。
因此,一个选项是使用{ggplotify}包中的as.ggplot()函数将基础图转换为ggplot对象,然后将其传递给ggarrange

b <- ggplotify::as.ggplot(~plot(res.hcpc, choice = "3D.map"))

#arranging in a single plot
ggarrange(a$gtable, b, labels = c("A", "B"))

相关问题