更改修改后的ggpairs图的轴标签(相关热图) [英] Change axis labels of a modified ggpairs plot (heatmap of correlation)

查看:101
本文介绍了更改修改后的ggpairs图的轴标签(相关热图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 ggpairs 图与热图结合起来,找到了一个不错的解决方案:

软件包版本:ggplot2 3.3.0,GGally 1.4.0

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.

解决方案

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

Packages versions: ggplot2 3.3.0, GGally 1.4.0

这篇关于更改修改后的ggpairs图的轴标签(相关热图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆