gpt4 book ai didi

r - 使用 scale_fill_gradientn() 将颜色比例转换为概率转换颜色分布

转载 作者:行者123 更新时间:2023-12-01 13:42:23 25 4
gpt4 key购买 nike

我正在尝试可视化严重拖尾的栅格数据,并且我想要颜色到值范围的非线性映射。有几个类似的问题,但它们并没有真正解决我的具体问题(见下面的链接)。

library(ggplot2)
library(scales)

set.seed(42)
dat <- data.frame(
x = floor(runif(10000, min=1, max=100)),
y = floor(runif(10000, min=2, max=1000)),
z = rlnorm(10000, 1, 1) )

# colors for the colour scale:
col.pal <- colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan", "#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000"))
fill.colors <- col.pal(64)

如果不以某种方式进行转换,则数据如下所示:
ggplot(dat, aes(x = x, y = y, fill = z)) +
geom_tile(width=2, height=30) +
scale_fill_gradientn(colours=fill.colors)

enter image description here
我的问题是与以下相关的后续问题
this onethis one ,并且给定 here 的解决方案实际上产生了我想要的图,除了图例:
qn <- rescale(quantile(dat$z, probs=seq(0, 1, length.out=length(fill.colors))))
ggplot(dat, aes(x = x, y = y, fill = z)) +
geom_tile(width=2, height=30) +
scale_fill_gradientn(colours=fill.colors, values = qn)

enter image description here

现在我希望图例中的色标表示值的非线性分布(现在只有标尺的红色部分可见),即图例也应该基于分位数。有没有办法做到这一点?

我认为色标中的 trans 参数可能会起作用,如建议的 here ,但这会引发错误,我认为是因为 qnorm(pnorm(dat$z)) 导致一些无限值(虽然我不完全理解该函数......)。
norm_trans <- function(){
trans_new('norm', function(x) pnorm(x), function(x) qnorm(x))
}
ggplot(dat, aes(x = x, y = y, fill = z)) +
geom_tile(width=2, height=30) +
scale_fill_gradientn(colours=fill.colors, trans = 'norm')
> Error in seq.default(from = best$lmin, to = best$lmax, by = best$lstep) : 'from' must be of length 1

那么,有没有人知道如何在情节和图例中具有基于分位数的颜色分布?

最佳答案

此代码将使用 pnorm 转换进行手动中断。这是你追求的吗?

ggplot(dat, aes(x = x, y = y, fill = z)) + 
geom_tile(width=2, height=30) +
scale_fill_gradientn(colours=fill.colors,
trans = 'norm',
breaks = quantile(dat$z, probs = c(0, 0.25, 1))
)

关于r - 使用 scale_fill_gradientn() 将颜色比例转换为概率转换颜色分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38874741/

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