使用scale_fill_gradientn()将颜色比例转换为概率转换颜色分布 [英] Transform color scale to probability-transformed color distribution with scale_fill_gradientn()
问题描述
我试图对重尾的栅格数据进行可视化处理,我想要将颜色非线性映射到值的范围。有几个类似的问题,但它们并不能真正解决我的具体问题(请参阅下面的链接)。
library(ggplot2)
库(比例)
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))
#颜色比例的颜色:
col.pal< - colorRampPalette(c(#00007F ,blue,#007FFF,青色,#7FFF7F,黄色,#FF7F00,红色,#7F0000))
fill.colors< - col .pa(64)
如果没有以某种方式转换数据, p>
ggplot(dat,aes(x = x,y = y,fill = z))+
geom_tile(width = 2,height = 30)+
scale_fill_gradientn(colors = fill.colors)
我的问题是与
相关的后续问题
现在我想让图例中的颜色比例代表值的非线性分布(现在只有比例尺的红色部分是可见的) ible),即传说也应该基于分位数。有没有办法做到这一点?
我认为色彩尺度内的 trans
参数可能会起作用,按照建议此处,但是会抛出错误,我想是因为 qnorm(pnorm(dat $ z))
会产生一些无限的值(尽管我完全不了解这个函数)。
pre $
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(colors = fill.colors,trans ='norm')
>在seq.default中出错(from = best $ lmin,to = best $ lmax,by = best $ lstep):'from'必须长度为1
那么,是否有人知道如何在图例中的plot 和中使用基于分位数的颜色分布?
此代码将使pnorm转换手动中断。这是你在追求什么?
ggplot(dat,aes(x = x,y = y,fill = z)) +
geom_tile(width = 2,height = 30)+
scale_fill_gradientn(colors = fill.colors,
trans ='norm',
breaks = quantile(dat $ z, probs = c(0,0.25,1))
)
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:
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), i.e. 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..).
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. 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))
)
这篇关于使用scale_fill_gradientn()将颜色比例转换为概率转换颜色分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!