gpt4 book ai didi

r - 更改修改后的 ggpairs 图的轴标签(相关热图)

转载 作者:行者123 更新时间:2023-12-04 10:19:27 25 4
gpt4 key购买 nike

我想结合一个 ggpairs用热图绘制并找到了一个很好的解决方案:
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"))

因为轴标签不适合我的真实数据,我想改变它们(角度),我找到了这个解决方案:

ggpairs rotate axis label

但是,如果我添加它,我会丢失热图
ggpairs(sample_df,
upper = list(continuous = my_fn),
lower = list(continuous = "smooth")) +
theme(axis.text.x = element_text(angle = 90, hjust = 1, size=8))

我也尝试将主题添加到 my_fn , 没有成功。

最佳答案

怎么样:

  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

软件包版本:ggplot2 3.3.0、GGally 1.4.0

关于r - 更改修改后的 ggpairs 图的轴标签(相关热图),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60930523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com