如何在一个面板中将 corrplot 混合矩阵与 ggplot2 图形结合起来?

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

我需要将

corrplot
包的基本图形图与
ggplot2
图形制作的图结合起来。这两个图形系统在 R 中不兼容,我找到了一些解决方法将它们添加到一个面板中,例如
patchwork
包或
ggplotify
包。然而,这里的情况略有不同,因为这些包一次处理一个基本图,将其转换为可以与任何 ggplot2 图形协调的 grob。 有一种特殊情况,需要在视口中将两个基本图相加,然后转换为 ggplot2 的 grob 对象。在这种情况下,例如,当您想要使用
corrplot
包使用
add = TRUE
参数创建相关矩阵时,这将简单地绘制一个图,当您查看它时,您运行第二个代码来添加后半部分使用
mtcars
数据得到一个完整的混合矩阵,如下所示。

corrplot 的混合相关矩阵

library(tidyverse)
library(corrplot)
library(patchwork)
data(mtcars)

M32 <- cor(mtcars)
M21 <- cor(mtcars[-c(3, 8, 9, 18, 19, 20, 21, 26, 27, 28, 32), ]) # all 4 cylinders cars were removed in this data

corrplot::corrplot(M32, method = "color",
                   type = "lower",
                   order = "original",
                   hclust.method = "ward.D2", 
                   col = COL2('PiYG'),
                   tl.pos = "lt", 
                   is.corr = FALSE,
                   diag = TRUE, 
                   tl.col = "black",
                   addrect = 2,
                   rect.col = "black",
                   addCoef.col = "white",
                   cl.cex = 0.8,
                   cl.align.text = 'l',
                   cl.pos = "b", 
                   number.cex = 0.75
                   )

corrplot::corrplot(M21, method = "color",
                   type = "upper",
                   order = "original",
                   add = TRUE, # this argument is important to combine the two halves of the matrix
                   hclust.method = "ward.D2", 
                   col = COL2('PiYG'),
                   tl.pos = "n", 
                   is.corr = FALSE,
                   diag = TRUE, 
                   tl.col = "black",
                   addrect = 2,
                   rect.col = "black",
                   addCoef.col = "white",
                   cl.cex = 0.8,
                   cl.align.text = 'l',
                   cl.pos = "n", 
                   number.cex = 0.75
                   )

corHybridPlot <- recordPlot()

非常重要的是,要获得这个混合矩阵,应该保留查看矩阵的前半部分,并运行第二个矩阵的代码的第二部分以获得一个完整的矩阵。 有时,需要执行

dev.off()
来关闭所有未打开的设备并重新开始才能正常工作。

现在让我们看看 ggplot2 的第二个图,我想将其添加到第一个图中。

Grpah 由 ggplot2

p1 <- ggplot(mtcars, aes(wt, mpg)) +
    geom_point(color = "red") +
    geom_text(aes(label = rownames(mtcars))) +
    theme_classic(base_size = 16)

我的问题

我认为拼凑是非常先进的,并且有很多花里胡哨的功能来满足出版质量的工作。那么,如何使用拼凑或您认为有效的任何其他方法将这两个图表组合在一个面板中?

r ggplot2 viewport r-corrplot patchwork
1个回答
0
投票

我认为

patchwork
的文档非常清楚。一种简单的选择是使用公式界面。例如,这有效:

p1 + ~my_plot()

我们可以将整个绘图结构写成公式,但在这里使用函数更容易。我们只需要定义

my_plot
即可完成您需要的所有基本绘图操作:

my_plot <- function() {
  M32 <- cor(mtcars)
  M21 <- cor(mtcars[-c(3, 8, 9, 18, 19, 20, 21, 26, 27, 28, 32), ]) # all 4 cylinders cars were removed in this data
  
  corrplot::corrplot(M32, method = "color",
                     type = "lower",
                     order = "original",
                     hclust.method = "ward.D2", 
                     col = COL2('PiYG'),
                     tl.pos = "lt", 
                     is.corr = FALSE,
                     diag = TRUE, 
                     tl.col = "black",
                     addrect = 2,
                     rect.col = "black",
                     addCoef.col = "white",
                     cl.cex = 0.8,
                     cl.align.text = 'l',
                     cl.pos = "b", 
                     number.cex = 0.75
  )
  
  corrplot::corrplot(M21, method = "color",
                     type = "upper",
                     order = "original",
                     add = TRUE, # this argument is important to combine the two halves of the matrix
                     hclust.method = "ward.D2", 
                     col = COL2('PiYG'),
                     tl.pos = "n", 
                     is.corr = FALSE,
                     diag = TRUE, 
                     tl.col = "black",
                     addrect = 2,
                     rect.col = "black",
                     addCoef.col = "white",
                     cl.cex = 0.8,
                     cl.align.text = 'l',
                     cl.pos = "n", 
                     number.cex = 0.75
  )
}

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.