简体   繁体   English

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

[英]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 onethis 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.

相关问题 为负值和正值定义颜色渐变 scale_fill_gradientn() - define color gradient for negative and positive values scale_fill_gradientn() 使用 scale_fill_gradientn 将特定颜色分配给条形 plot 中的确定值 - Assign specific color to definite value in bar plot using scale_fill_gradientn 使用转换后的颜色/填充变量 ggplot2 为 scale_*_gradientn 指定手动值 - Specify manual values for scale_*_gradientn with transformed color/fill variable ggplot2 在 scale_fill_gradientn 中使用绝对值而不是相对值 - Use absolute values not relative values in scale_fill_gradientn 用ggplot2的scale_fill_gradientn表示,恒定渐变 - Representation with, scale_fill_gradientn ggplot2, constant gradient 如何获得多个具有相同比例的 ggplot2 scale_fill_gradientn? - How to get multiple ggplot2 scale_fill_gradientn with same scale? R:使用带有geom_tile()和scale_fill_gradientn()的颜色划分热图 - R: Dividing heat map using colors with geom_tile() and scale_fill_gradientn() ggplot手动设置scale_color_gradientn - ggplot set scale_color_gradientn manually scale_color_gradientn()无法产生正确的输出 - scale_color_gradientn() not producing the correct output 在热图中为比例设置 scale_fill_gradientn 的限制(从 0 到 1 的值带有蓝色刻度,0 是白色,大于 1 的值带有红色刻度) - Set limits for scale_fill_gradientn for ratios (values from 0 to 1 with blue scale, 0 is white and values over 1 with red scale) in heatmap
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM