I am using ggcorrplot2 (github page) to generate my correlation plots, since I need to overlay significance levels as *** on top.
This package relies on ggplot2, so I thought it would be easy to change different features like axis label font size, asterisk color, gradient colors, etc. But it is proving to be more complicated than I thought.
My current problem at hand is that the "x-axis" labels get cropped out of the plotting area... As you see below, this isn't actually the x-axis, but rather labels placed on top of the diagonal cells. Hence, it is quite difficult to change them.
Check out this MWE. I first did this:
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()
Which produces this:

As you can see, I was able to modify the y-axis labels with ggplot2 theme, but not the "x-axis" labels or anything else...
So I figured I could use ggplot_build and tweak the plot before actually printing it, and I did the following:
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()
Which produces this:

Very close, but still not quite there yet. The problems I keep encountering are the following:
I attempted to solve this by adding plot.margin=grid::unit(c(0,3,0,0),"cm") to theme, but the result is this (still cropped label and more grey space on top and bottom of the plot):

Any help? Thanks!
In the original function, the author set expand = c(0, 0) in scale_x_continuous(). You just need to modify that part to get what you want
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.

Created on 2020-09-01 by the reprex package (v0.3.0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With