3

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:

test1

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:

test2

Very close, but still not quite there yet. The problems I keep encountering are the following:

  • The "x-axis" labels get cropped
  • Weird grey area on top and bottom of the plot
  • I want to change the color gradient so the darker blue and darker red aren't that dark

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

test3

Any help? Thanks!

DaniCee
  • 2,397
  • 6
  • 36
  • 59

1 Answers1

2

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)

Tung
  • 26,371
  • 7
  • 91
  • 115
  • Thanks! Let me try it out, and see also if I can change the gradient easily with `ggplot2::scale_fill_gradientn()` – DaniCee Sep 02 '20 at 07:53
  • Any possibility of removing the `Scale for 'x' is already present. Adding another scale for 'x', which will replace the existing scale` warning? – DaniCee Sep 02 '20 at 09:35
  • By the way, what's with that extra grey area added on the top and bottom of the plot? Thanks! – DaniCee Sep 02 '20 at 09:37
  • @DaniCee: you should create a new function based on the `ggcorplot2` then you can change the default [`col2 <- grDevices::colorRampPalette(RColorBrewer::brewer.pal(n = 11, name = "RdBu"))`](https://github.com/caijun/ggcorrplot2/blob/master/R/ggcorrplot.R#L158). Some good divergent colormap are here https://stackoverflow.com/questions/37482977/what-is-a-good-palette-for-divergent-colors-in-r-or-can-viridis-and-magma-b/52812120#52812120 – Tung Sep 02 '20 at 16:09
  • For the warning: you can either modify the `ggcorplot2` function directly or use `suppressWarnings()` around the `print()` call https://stackoverflow.com/questions/13286531/how-to-suppress-warnings-when-plotting-with-ggplot – Tung Sep 02 '20 at 16:12
  • I'm not sure about the extra grey area. Maybe try `ggsave()` instead? Example [here](https://stackoverflow.com/a/51906008/786542) – Tung Sep 02 '20 at 16:15
  • the `suppressWarnings()` is what I did first, but the warning happens on the `ggcorrplot()` call, not on `print()`... – DaniCee Sep 03 '20 at 02:51