gpt4 book ai didi

r - 带有 corrplot 的非均匀色标

转载 作者:行者123 更新时间:2023-12-04 07:42:03 34 4
gpt4 key购买 nike

是否可以在 corrplot 的色标中使用非均匀中断?下面的代码

library(RColorBrewer)
library(corrplot)
M <- matrix(runif(25,min=-1,max=1),nrow=5)
corrplot(M,is.corr=FALSE,cl.lim = c(-1, 1),col = brewer.pal(n = 6, name = "RdBu"))
生产 a plot like this one.
您可以看到颜色在 -1 和 1 之间平均分布。是否有可能在指定位置发生这些中断?例如说 0.2 和 0.8,而不是 0.33 和 0.66?
编辑:此外,是否可以在每个中断旁边指定文本,而不是数值?或者沿着颜色条移动该文本位置。我想除了像 cl.lim 等提供的选项之外,我只是不确定如何访问 colorbar 本身的设置。
提前致谢!

最佳答案

据我所知,没有任何方法可以改变 corrplot 的这些方面,抱歉。一种潜在的替代方法是使用 ggplot 绘制相关性并通过“比例”更改中断/标签,例如

library(tidyverse)
library(corrplot)
library(psych)

M <- matrix(runif(25, min = -1, max = 1), nrow = 5)
rownames(M) <- 1:5
colnames(M) <- 1:5
df <- corr.test(M)

df$r %>%
as.data.frame() %>%
rownames_to_column("id") %>%
pivot_longer(-c(id), names_to = "samples", values_to = "Correlation") %>%
ggplot() +
geom_raster(aes(x = samples, y = id, fill = Correlation)) +
scale_fill_distiller(palette = "RdBu",
breaks = c(-0.8, -0.2, 0.2, 0.8),
labels = c("strong -ve corr",
"weak -ve corr",
"weak +ve corr",
"strong +ve corr"))
example_1.png
或者,如果要将特定范围之外的值的方块着色为灰色:
df$r %>%
as.data.frame() %>%
rownames_to_column("id") %>%
pivot_longer(-c(id), names_to = "samples", values_to = "Correlation") %>%
ggplot() +
geom_raster(aes(x = samples, y = id, fill = Correlation)) +
scale_fill_distiller(palette = "RdBu",
breaks = c(-0.8, -0.2, 0.2, 0.8),
labels = c("strong -ve corr",
"weak -ve corr",
"weak +ve corr",
"strong +ve corr"),
limits = c(-1, 0.99))
example_2.png

关于r - 带有 corrplot 的非均匀色标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67411783/

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