[英]R correlation plot using ggcorrplot2: "x-axis" labels get cropped
我正在使用ggcorrplot2
( github 頁)來生成我的相關圖,因為我需要將顯着性水平疊加為***
在上面。
這個 package 依賴於ggplot2
,所以我認為更改軸 label 字體大小、星號顏色、漸變 colors 等不同功能很容易。但事實證明它比我想象的要復雜。
我目前手頭的問題是“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
並在實際打印之前調整 plot,我做了以下操作:
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()
產生這個:
非常接近,但還沒有完全實現。 我不斷遇到的問題如下:
我試圖通過將plot.margin=grid::unit(c(0,3,0,0),"cm")
添加到theme
來解決這個問題,但結果是這樣的(仍然裁剪了 label 並且頂部和頂部有更多灰色空間情節的底部):
有什么幫助嗎? 謝謝!
在最初的 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-09-01
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.