Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
763 views
in Technique[技术] by (71.8m points)

r - ggpairs plot with heatmap of correlation values

My question is twofold;

I have a ggpairs plot with the default upper = list(continuous = cor) and I would like to colour the tiles by correlation values (exactly like what ggcorr does).

I have this: ggpairs plot of daily flows
I would like the correlation values of the plot above to be coloured like this: ggcorr heatmap of correlation values

library(GGally)

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

ggpairs(sample_df, lower = list(continuous = "smooth"))  
ggcorr(sample_df, label = TRUE, label_round = 2)

I had a brief go at trying to use upper = list(continuous = wrap(ggcorr) but didn't have any luck and, given that both functions return plot calls, I don't think that's the right path?

I am aware that I could build this in ggplot (e.g. Sandy Muspratt's solution) but given that the GGally package already has the functionality I am looking for I thought I might be overlooking something.


More broadly, I would like to know how we, or if we can, call the correlation values? A simpler option may be to colour the labels rather than the tile (i.e. this question using colour rather than size) but I need a variable to assign to colour...

Being able to call the correlation values to use in other plots would be handy although I suppose I could just recalculate them myself.

Thank you!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A possible solution is to get the list of colors from the ggcorr correlation matrix plot and to set these colors as background in the upper tiles of the ggpairs matrix of plots.

library(GGally)   
library(mvtnorm)
# Generate data
set.seed(1)
n <- 100
p <- 7
A <- matrix(runif(p^2)*2-1, ncol=p) 
Sigma <- cov2cor(t(A) %*% A)
sample_df <- data.frame(rmvnorm(n, mean=rep(0,p), sigma=Sigma))
colnames(sample_df) <- c("KUM", "MHP", "WEB", "OSH", "JAC", "WSW", "gaugings")

# Matrix of plots
p1 <- ggpairs(sample_df, lower = list(continuous = "smooth"))  
# Correlation matrix plot
p2 <- ggcorr(sample_df, label = TRUE, label_round = 2)

The correlation matrix plot is:

enter image description here

# Get list of colors from the correlation matrix plot
library(ggplot2)
g2 <- ggplotGrob(p2)
colors <- g2$grobs[[6]]$children[[3]]$gp$fill

# Change background color to tiles in the upper triangular matrix of plots 
idx <- 1
for (k1 in 1:(p-1)) {
  for (k2 in (k1+1):p) {
    plt <- getPlot(p1,k1,k2) +
     theme(panel.background = element_rect(fill = colors[idx], color="white"),
           panel.grid.major = element_line(color=colors[idx]))
    p1 <- putPlot(p1,plt,k1,k2)
    idx <- idx+1
}
}
print(p1)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...