[英]Transform color scale to probability-transformed color distribution with scale_fill_gradientn()
I am trying to visualize heavily tailed raster data, and I would like a non-linear mapping of colors to the range of the values.我正在尝试可视化严重拖尾的栅格数据,并且我想要颜色到值范围的非线性映射。 There are a couple of similar questions, but they don't really solve my specific problem (see links below).有几个类似的问题,但它们并没有真正解决我的具体问题(见下面的链接)。
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)
This is how the data look like if not transformed in some way:如果不以某种方式进行转换,则数据如下所示:
ggplot(dat, aes(x = x, y = y, fill = z)) +
geom_tile(width=2, height=30) +
scale_fill_gradientn(colours=fill.colors)
My question is sort of a follow-up question related to this one or this one , and the solution given here actually yields exactly the plot I want, except for the legend:我的问题是与this one或this one相关的后续问题,这里给出的解决方案实际上产生了我想要的情节,除了图例:
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)
Now I want the colour scale in the legend to represent the non-linear distribution of the values (now only the red part of the scale is visible), ie the legend should as well be based on quantiles.现在我希望图例中的色标表示值的非线性分布(现在只有标尺的红色部分可见),即图例也应该基于分位数。 Is there a way to accomplish this?有没有办法做到这一点?
I thought the trans
argument within the colour scale might do the trick, as suggested here , but that throws an error, I think because qnorm(pnorm(dat$z))
results in some infinite values (I don't completely understand the function though..).我认为色标中的trans
参数可能会qnorm(pnorm(dat$z))
,正如这里建议的那样,但这会引发错误,我认为是因为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
So, does anybody know how to have a quantile-based colour distribution in the plot and in the legend?那么,有没有人知道如何在情节和图例中具有基于分位数的颜色分布?
This code will make manual breaks with a pnorm transformation. 此代码将通过pnorm转换进行手动中断。 Is this what you are after? 这就是你追求的吗?
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))
)
I believe you have been looking for a quartile transform.我相信你一直在寻找四分位变换。 Unfortunately there is none in scales
, but it is not to hard to build one yourself (on the fly):不幸的是,没有scales
,但自己构建一个并不难(即时):
make_quantile_trans <- function(x) {
name <- paste0("quantiles_of_", deparse1(substitute(x)))
xs <- sort(x)
N <- length(xs)
transform <- function(x) findInterval(x, xs)/N # find the last element that is smaller
inverse <- function(q) xs[1+floor(q*(N-1))]
scales::trans_new(
name = name,
transform =transform,
inverse = inverse,
breaks = function(x, n = 5) inverse(extended_breaks()(transform(x), n)),
minor_breaks = function(x, n = 5) inverse(regular_minor_breaks()(transform(x), n)),
format = scales::label_number(),
domain = xs[c(1, N)] + c(-1, 1)
)
}
ggplot(dat, aes(x = x, y = y, fill = z)) +
geom_tile(width=2, height=30) +
scale_fill_gradientn(colours=fill.colors, trans = make_quantile_trans(dat$z))
Created on 2021-11-12 by the reprex package (v2.0.1)由reprex 包(v2.0.1) 于 2021 年 11 月 12 日创建
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.