2

I wanted to combine a ggpairs plot with a heatmap and found a wonderful solution: ggpairs plot with heatmap of correlation values

#library
library(GGally)
library(ggplot2)

#data
sample_df <- data.frame(replicate(7,sample(0:5000,100)))
colnames(sample_df) <- c("KUM", "MHP", "WEB", "OSH", "JAC", "WSW", "gaugings")

#function for heatmap
my_fn <- function(data, mapping, method="p", use="pairwise", ...){

          # grab data
          x <- eval_data_col(data, mapping$x)
          y <- eval_data_col(data, mapping$y)

          # calculate correlation
          corr <- cor(x, y, method=method, use=use)

          # calculate colour based on correlation value
          # Here I have set a correlation of minus one to blue, 
          # zero to white, and one to red 
          # Change this to suit: possibly extend to add as an argument of `my_fn`
          colFn <- colorRampPalette(c("blue", "white", "red"), interpolate ='spline')
          fill <- colFn(100)[findInterval(corr, seq(-1, 1, length=100))]

          ggally_cor(data = data, mapping = mapping, ...) + 
            theme_void() +
            theme(panel.background = element_rect(fill=fill))
        }

#combine
ggpairs(sample_df, 
                   upper = list(continuous = my_fn),
                   lower = list(continuous = "smooth"))

Because the axis labels do not fit in my real data, I would like to change them (angle) and I found this solution:

ggpairs rotate axis label

However, If I add it, I lose the heatmap

ggpairs(sample_df,
        upper = list(continuous = my_fn),
        lower = list(continuous = "smooth")) +
        theme(axis.text.x = element_text(angle = 90, hjust = 1, size=8))

I also tried to add the theme to the my_fn, without success.

ava
  • 840
  • 5
  • 19

1 Answers1

3

How about:

  ggpairs(sample_df, 
        upper = list(continuous = my_fn),
        lower = list(continuous = "smooth"))+
  theme(axis.text.x = element_text(angle = 90, hjust = 1, size=8))

enter image description here

Packages versions: ggplot2 3.3.0, GGally 1.4.0

johnjohn
  • 716
  • 2
  • 9
  • 17
  • It's working for me. In your question above, it's ggpairs(my data...). Shouldn't it be ggpairs(sample_df...)? The heatmap looks fine when I run this. – johnjohn Mar 30 '20 at 17:37
  • @johnjohn; what version of GGally are you using pleae? – user20650 Mar 30 '20 at 17:42
  • @user20650 3.6.3 – johnjohn Mar 30 '20 at 17:45
  • @user20650 oops, sorry about that. It's version 1.4.0 – johnjohn Mar 30 '20 at 17:49
  • 1
    Thanks. I cannot reproduce your results of "working for me" on 1.4.0 or 1.5.0: the colours from the panel are removed . However, for my system I can *fix* it by removing the `theme_void` and adding the lines in my comment [here](https://stackoverflow.com/questions/45873483/ggpairs-plot-with-heatmap-of-correlation-values#comment107808271_53685979) – user20650 Mar 30 '20 at 17:51
  • @user20650 how do I send a copy of my output in comments? I do have colors as well using the code above, although they are less bright--but not different than in the version without the angle changed. – johnjohn Mar 30 '20 at 17:56
  • @user20650 done. As I was saying, the colors are not bright, but there are colors. – johnjohn Mar 30 '20 at 18:00
  • 1
    cheers @johnjohn; Well it seems clear that the original code is working as expected for you. – user20650 Mar 30 '20 at 18:04
  • out of curiosity what ggplot version ; `packageVersion("ggplot2")` – user20650 Mar 30 '20 at 18:07
  • The original code does not work for me neither. the fix mentioned above solved it. My ggplot2 version is ‘3.2.1’ – ava Mar 30 '20 at 18:09
  • ditto @ava. Ill try ggplot 3.3.0 – user20650 Mar 30 '20 at 18:10
  • @user20650 many thanks. I'm using 3.3.0. Also I see you are an advanced user, so how do you explain that the colors are not brigther? Overplotting? I've noticed that in other correlations plots, however I had assumed that it's because the coefficients are rather low (just as they are in this case, btw. indeed none is above .3) – johnjohn Mar 30 '20 at 18:12
  • 2
    okay; @ava and johnjohn I upgraded to ggplot2 v 3.3.0 and the original code still works. – user20650 Mar 30 '20 at 18:14
  • @johnjohn; the colours are likely dull due to the low correlations --> you could change the range of colours in `colorRampPalette` (or whatever function you use to generate the colour scheme) . John it may be worth adding package versions to your question – user20650 Mar 30 '20 at 18:16
  • 2
    @user20650 I've now added packages versions. – johnjohn Mar 30 '20 at 18:26