【问题标题】:R correlation plot using ggcorrplot2: "x-axis" labels get cropped使用 ggcorrplot2 的 R 相关图:“x 轴”标签被裁剪
【发布时间】:2020-12-21 05:04:29
【问题描述】:

我正在使用ggcorrplot2 (github page) 来生成我的相关图,因为我需要将显着性水平叠加为顶部的***

这个包依赖于ggplot2,所以我认为改变轴标签字体大小、星号颜色、渐变颜色等不同的特性很容易。但事实证明它比我想象的要复杂。

我目前手头的问题是“x 轴”标签从绘图区域中裁剪出来......如下所示,这实际上不是 x-axis,而是放置在顶部的标签对角线单元格。因此,很难改变它们。

看看这个 MWE。我第一次这样做:

data(mtcars)
#change "wt" to a very long name
names(mtcars)[6] <- "a very long name"

corrtest <- psych::corr.test(mtcars[,1:7], adjust="none")
all_matrix <- corrtest$r
all_pmat <- corrtest$p

###

P <- ggcorrplot2::ggcorrplot(all_matrix, type = "lower", method = "circle", p.mat = all_pmat, show.diag = FALSE,
                             insig = "label_sig", sig.lvl = c(0.05, 0.01, 0.001), pch = "*", pch.cex = 6) +
     ggplot2::theme(axis.text.y=ggplot2::element_text(size=15),
                    legend.text=ggplot2::element_text(size=15))
grDevices::pdf(file="heat_all2.pdf", height=6, width=6)
print(
  P
)
grDevices::dev.off()

产生这个:

如您所见,我可以用ggplot2 主题修改y-axis 标签,但不能修改“x 轴”标签或其他任何东西...

所以我想我可以使用ggplot_build 并在实际打印之前调整绘图,然后我做了以下操作:

P <- ggcorrplot2::ggcorrplot(all_matrix, type = "lower", method = "circle", p.mat = all_pmat, show.diag = FALSE,
                             insig = "label_sig", sig.lvl = c(0.05, 0.01, 0.001), pch = "*", pch.cex = 6) +
     ggplot2::theme(axis.text.y=ggplot2::element_text(size=15),
                    legend.text=ggplot2::element_text(size=15))
P2 <- ggplot2::ggplot_build(P)
P2$data[[4]]$size <- 5
P2$data[[4]]$hjust <- 0
P2$data[[3]]$angle <- 15
P2$data[[3]]$colour <- "grey30"
grDevices::pdf.options(reset = TRUE, onefile = FALSE)
grDevices::pdf(file="heat_all2.pdf", height=6, width=6)
print(
  graphics::plot(ggplot2::ggplot_gtable(P2))
)
grDevices::dev.off()

产生这个:

非常接近,但还没有完全达到。我一直遇到的问题如下:

  • “x 轴”标签被裁剪
  • 绘图顶部和底部的奇怪灰色区域
  • 我想改变颜色渐变,使深蓝色和深红色 不是那么黑暗

我试图通过将plot.margin=grid::unit(c(0,3,0,0),"cm") 添加到theme 来解决这个问题,但结果是这样的(标签仍然被裁剪,图的顶部和底部有更多的灰色空间):

有什么帮助吗?谢谢!

【问题讨论】:

    标签: r ggplot2 correlation axis-labels area


    【解决方案1】:

    original function中,作者在scale_x_continuous()中设置了expand = c(0, 0)。您只需要修改该部分即可获得所需的内容

    library(tidyverse)
    library(ggcorrplot2)
    
    data(mtcars)
    # change "wt" to a very long name
    names(mtcars)[6] <- "a very long name"
    
    corrtest <- psych::corr.test(mtcars[, 1:7], adjust = "none")
    all_matrix <- corrtest$r
    all_pmat <- corrtest$p
    
    ###
    P <- ggcorrplot2::ggcorrplot(all_matrix,
      type = "lower", method = "circle", p.mat = all_pmat, show.diag = FALSE,
      insig = "label_sig", sig.lvl = c(0.05, 0.01, 0.001), pch = "*", pch.cex = 6) +
      ggplot2::theme(axis.text.y = ggplot2::element_text(size = 15),
        legend.text = ggplot2::element_text(size = 15))
    
    P +
      scale_x_continuous(expand = expansion(mult = c(0, 0.25)))
    #> Scale for 'x' is already present. Adding another scale for 'x', which will
    #> replace the existing scale.
    

    reprex package (v0.3.0) 于 2020 年 9 月 1 日创建

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 2011-06-02
    • 1970-01-01
    • 2020-06-28
    相关资源
    最近更新 更多