gpt4 book ai didi

r - 将 corrplot 输出转换为 grob

转载 作者:行者123 更新时间:2023-12-02 20:10:24 24 4
gpt4 key购买 nike

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

Error in gList(list(1, 0.45, 0.62, 0.55, 0.68, 0.64, -0.13, -0.37, -0.22, : only 'grobs' allowed in "gList" In addition: Warning message: In grob$wrapvp <- vp : Coercing LHS to a list

数据:

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

最佳答案

gridGraphics 包中的

grid.echo + 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

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

original size looks fine

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

larger size with squares that don't cover the matrix

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

smaller size with squares that spill out

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

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 中的所有其他子对象都会同步调整,除了负责颜色的对象之外。

解决方案:

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

如果您使用 corrplot 的默认方法,请将 gPath("square") 替换为 gPath("circle")。我还没有测试相应的 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 中的文本大小参数。根据您当前的代码,标签显得相当大,并且当您将多个图排列在一起时很容易被切断。

关于r - 将 corrplot 输出转换为 grob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53734543/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com