我试图使用grid.arrange组合多种类型的图形/表,其中之一是使用corrplot的相关矩阵。是否有一种方法可以将corr图转换为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中的错误(列表(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"))发布于 2018-12-12 04:33:05
grid.echo + grid.grab来自gridGraphics包,将corrplot绘制的图形转换为外观相同的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生成的图形相同)

较大的大小(有色区域保持方形,即使矩阵已扩展到矩形单元格,也不要扩展到每个单元格的边缘):

较小的尺寸(有色区域的最小高度/宽度,使它们超出每个细胞的范围):

如果我们把多个grobs安排在一起,它几乎肯定会看起来很奇怪:
library(gridExtra)
grid.arrange(P1, P1, P1, layout_matrix = matrix(c(1, 1, 2, 3), nrow = 2, ncol = 2))

简而言之,由于corrplot绘制图形的方式,当图形设备调整大小时,P1中的所有其他子grobs都会同步调整,但负责颜色的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))

顺便说一下,您可能需要调整corrplot中的文本大小参数。根据您当前的代码,标签看起来相当大,当您将多个情节排列在一起时,标签可能会被切断。
https://stackoverflow.com/questions/53734543
复制相似问题