将corrplot输出转换为grob

时间:2018-12-12 00:51:25

标签: r r-corrplot grob

我正在尝试使用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)
  )
  

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"))

1 个答案:

答案 0 :(得分:1)

gridGraphics软件包中的

grid.echo + grid.grabcorrplot绘制的图形转换为外观相同的grob。麻烦的是,该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

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

original size looks fine

较大的尺寸(即使矩阵已扩展到矩形单元,且未扩展到每个单元的边缘,彩色区域仍为正方形):

larger size with squares that don't cover the matrix

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

smaller size with squares that spill out

如果我们将多个杂点排列在一起,几乎可以肯定看起来很奇怪:

library(gridExtra)
grid.arrange(P1, P1, P1, layout_matrix = matrix(c(1, 1, 2, 3), nrow = 2, ncol = 2))

arranging 3 plots together

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

解决方案

# 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))

如果使用gPath("square")的默认方法,请将gPath("circle")替换为corrplot。对于相应的grob名称,我尚未测试其他方法选项,但一般原理应该相似。

检查是否所有内容都已对齐

grid.arrange(P1, P1, P1, layout_matrix = matrix(c(1, 1, 2, 3), nrow = 2, ncol = 2))

arranging 3 plots together after applying solution

顺便说一句,您可能需要调整corrplot中的文本大小参数。根据您当前的代码,标签看起来相当大,并且当您将多个地块放在一起时很容易被剪掉。