Change axis labels of a modified ggpairs plot (heatmap of correlation)
Asked Answered
C

1

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.

Condemnatory answered 30/3, 2020 at 12:25 Comment(0)
W
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

Winfrid answered 30/3, 2020 at 17:19 Comment(15)
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.Winfrid
@johnjohn; what version of GGally are you using pleae?Disparate
@Disparate 3.6.3Winfrid
@Disparate oops, sorry about that. It's version 1.4.0Winfrid
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 hereDisparate
@Disparate 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.Winfrid
@Disparate done. As I was saying, the colors are not bright, but there are colors.Winfrid
cheers @johnjohn; Well it seems clear that the original code is working as expected for you.Disparate
out of curiosity what ggplot version ; packageVersion("ggplot2")Disparate
The original code does not work for me neither. the fix mentioned above solved it. My ggplot2 version is ‘3.2.1’Condemnatory
ditto @ava. Ill try ggplot 3.3.0Disparate
@Disparate 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)Winfrid
okay; @Condemnatory and johnjohn I upgraded to ggplot2 v 3.3.0 and the original code still works.Disparate
@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 questionDisparate
@Disparate I've now added packages versions.Winfrid

© 2022 - 2024 — McMap. All rights reserved.