将 corrplot 输出转换为 grob

Gar*_*rth 2 r grob r-corrplot

我正在尝试使用 grid.arrange 组合多种类型的图形/表格,其中之一是使用corrplot. 有没有办法将 corrplot 转换为 grob 或导出/导入为与 兼容的图像grid.arrange?由于我组合的其他图来自 ggplot 和 tableGrob,我似乎无法使用par(mfrow = c(2, 2))layout(matrix(1:2))按照其他帖子中的建议。

P1 <- corrplot(PANAcor, order="hclust", addgrid.col = "gray",  
               type="full", col = col2(50), tl.cex=1.5, tl.col="black", 
               method="color", tl.pos="lt", tl.srt=45, hclust.method = "average",
               cl.ratio = 0.25, cl.align = "l", number.cex = 2)

summary <- grid.arrange(
    top=textGrob(sprintf("%s Summary",subject), gp=gpar(fontsize=16,font=8)),
    blank, P1, P2,
    blank, T1, T2,
    ncol=3, widths = c(0.1, 3, 3), 
    nrow=2, heights= c(1, 1),
    bottom = textGrob(sprintf("%s run %s",version,runtime), 
    gp=gpar(fontsize=6,font=8), hjust=-1)
  )
Run Code Online (Sandbox Code Playgroud)

gList(list(1, 0.45, 0.62, 0.55, 0.68, 0.64, -0.13, -0.37, -0.22, 中的错误:“gList”中仅允许“grobs”另外:警告消息:在 grob$wrapvp <- vp 中:将 LHS 强制添加到列表中

数据:

PANAcor <- structure(c(1, 0.56, 0.68, -0.49, -0.4, -0.39, 0.56, 1, 0.64, -0.55, 
                       -0.49, -0.54, 0.68, 0.64, 1, -0.69, -0.57, -0.65, -0.49,
                       -0.55, -0.69, 1, 0.82, 0.73, -0.4, -0.49, -0.57, 0.82, 1, 
                       0.71, -0.39, -0.54, -0.65, 0.73, 0.71, 1), 
                     .Dim = c(6L, 6L), 
                     .Dimnames = list(c("Anxious", "Irritable", "Upset", "Happy",
                                        "Enthusiastic", "Outgoing"), 
                                      c("Anxious", "Irritable", "Upset", "Happy", 
                                        "Enthusiastic", "Outgoing"))) 

col2 <- colorRampPalette(c("#7bffff","#7bbdff","#0000ff","black",
                           "#ff1a1a","#ff8000","#ffff4d"))
Run Code Online (Sandbox Code Playgroud)

Z.L*_*Lin 5

grid.echo来自 gridGraphics 包的+grid.grab会将绘制的图形转换corrplot为外观相同的 grob。问题是,只有在完全相同的图形设备尺寸下,该对象看起来才相同。

重现问题

library(gridGraphics)
library(grid)

corrplot(PANAcor, order="hclust", addgrid.col = "gray",  
         type="full", col = col2(50), tl.cex=1.5, tl.col="black", 
         method="color", tl.pos="lt", tl.srt=45, hclust.method = "average",
         cl.ratio = 0.25, cl.align = "l", number.cex = 2)

## grab the scene as a grid object & save it to P1
grid.echo()
P1 <- grid.grab()

grid.draw(P1) # looks fine, until you resize the graphics device
Run Code Online (Sandbox Code Playgroud)

原始尺寸(看起来与生成的图形相同corrplot

原来的尺寸看起来不错

更大的尺寸(即使矩阵已扩展到矩形单元格,彩色区域仍保持正方形,并且不会扩展到每个单元格的边缘):

较大尺寸的正方形不覆盖矩阵

较小的尺寸(彩色区域具有最小高度/宽度,这导致它们溢出到每个单元格的范围之外):

较小的尺寸,带有溢出的正方形

如果我们将多个对象排列在一起,几乎肯定会看起来很奇怪:

library(gridExtra)
grid.arrange(P1, P1, P1, layout_matrix = matrix(c(1, 1, 2, 3), nrow = 2, ncol = 2))
Run Code Online (Sandbox Code Playgroud)

将 3 个地块排列在一起

简而言之,由于corrplot绘制图形的方式,当图形设备调整大小时,P1 中的所有其他子对象都会同步调整,除了负责颜色的对象之外。

解决方案

# save correlation matrix colors to a vector, then make coloured matrix grob transparent
matrix.colors <- getGrob(P1, gPath("square"), grep = TRUE)[["gp"]][["fill"]]
P1 <- editGrob(P1,
               gPath("square"), grep = TRUE,
               gp = gpar(col = NA,
                         fill = NA))

# apply the saved colours to the underlying matrix grob
P1 <- editGrob(P1,
               gPath("symbols-rect-1"), grep = TRUE,
               gp = gpar(fill = matrix.colors))

# convert the background fill from white to transparent, while we are at it
P1 <- editGrob(P1,
               gPath("background"), grep = TRUE,
               gp = gpar(fill = NA))
Run Code Online (Sandbox Code Playgroud)

如果您使用 的默认方法,请替换gPath("square")为。我还没有测试相应的 grob 名称的其他方法选项,但总体原理应该类似。gPath("circle")corrplot

检查现在一切是否已对齐

grid.arrange(P1, P1, P1, layout_matrix = matrix(c(1, 1, 2, 3), nrow = 2, ncol = 2))
Run Code Online (Sandbox Code Playgroud)

应用解决方案后将 3 个地块排列在一起

顺便说一句,您可能想调整 中的文本大小参数corrplot。根据您当前的代码,标签显得相当大,并且当您将多个图排列在一起时很容易被切断。