2d中的颜色梯度图 [英] 2d color gradient plot in R

查看:195
本文介绍了2d中的颜色梯度图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要产生一个2d颜色梯度矩形,如下图右侧的图片。我如何在R中这样做?使用 colorRamp RColorBrewer 或其他功能/包我可以产生漂亮的1D彩色斜坡。但是我怎么做这个2D包括几个颜色在角落,如。



p>

我想得到的是eg以下两种渐变类型:






BTY:我完全忘了提到我发现上面的图表

感谢您对我的帖子发表评论 - 我很高兴它产生了一些讨论。
这里是一个最小的代码实现右上角的图 - 我相信有其他更有效的方法来做它...但这个工作不需要其他库,应该很容易遵循.. 。您可以通过使用max_sat和alpha_default变量来更改饱和度和Alpha混合...

  #define颜色渐变的极值
rampk2r < - colorRampPalette(c(rgb(0/255,0 / 255,0 / 255),rgb(218/255,0 / 255,0 / 255)))
rampk2g< - colorRampPalette(c(rgb(0/255,0/255,0/255),rgb(0/255,218/255,0/255)))

#跨越数字到0,1间隔
prop< - function(x,lo = 0,hi = 100){
if(is.na(x)){NA}
else {
min(lo,hi)+ x *(max(lo,hi)-min(lo,hi))
}
}

rangepropCA< -c(0,20)
rangepropCB <-c(0,20)

#定义一些默认变量
if(!exists('alpha_default')){alpha_default& -1}默认为#不透明颜色
if(!exists('palette_l')){palette_l< -50}#调色板中有多少步
if(!exists('max_sat')){ max_sat <-200}#最大饱和度
colorpalette <-0:palette_l *(max_sat / 255)/ palette_l#她的最终调色板...

#首先做一个empy图
plot(NULL,xlim = rangepropCA,ylim = rangepropCB,log ='',xaxt ='n',yaxt ='n',xlab ='prop A',ylab ='prop B',bty = n',main ='color field');
#然后用不同的颜色填充矩形
for(m in 1:palette_l){
for(n in 1:palette_l){
rgbcol< -rgb(colorpalette [n],colorpalette [m],0,alpha_default);
rect(xleft = prop(x =(n-1)/(palette_l),rangepropCA [1],rangepropCA [2])
,xright = prop(x =(n)/(palette_l) ,rangepropCA [1],rangepropCA [2])
,ytop = prop(x =(m-1)/(palette_l),rangepropCB [1],rangepropCB [2] (x =(m)/(palette_l),rangepropCB [1],rangepropCB [2])
,col = rgbcol
,border =transparent

} b $ b}
#done!


I want to produce a 2d color gradient rectangle like the ones in the picture below on the right hand side. How can I do this in R? Using colorRamp or RColorBrewer or other functions/packages I can produce nice 1D dolor ramps. But how do I do this for 2D including several colors in the corners, like e.g. the upper right rectangle?

What I want to get is e.g. the following two gradient types:

BTY: I completely forgot to mention that I found the above chart here (produced by Luca Fenu).

解决方案

Thanks for commenting on my post - I'm glad it generated some discussion. Here's a minimal code to achieve the plots on the upper right - I'm sure there's other more efficient ways to do it... But this works without need for other libraries, and should be easy enough to follow... you can change saturation and alpha blending by playing with the max_sat and alpha_default variables...

#define extremes of the color ramps
rampk2r <- colorRampPalette(c(rgb(  0/255,   0/255,   0/255), rgb(218/255,   0/255,   0/255)))
rampk2g <- colorRampPalette(c(rgb(  0/255,   0/255,   0/255), rgb(  0/255, 218/255,   0/255)))

# stupid function to reduce every span of numbers to the 0,1 interval
prop <- function(x, lo=0, hi=100) {
    if (is.na(x)) {NA}
    else{
        min(lo,hi)+x*(max(lo,hi)-min(lo,hi))
    }
}

rangepropCA<-c(0,20)
rangepropCB<-c(0,20)

# define some default variables
if (!exists('alpha_default')) {alpha_default<-1} # opaque colors by default
if (!exists('palette_l')) {palette_l<-50} # how many steps in the palette
if (!exists('max_sat')) {max_sat<-200} # maximum saturation
colorpalette<-0:palette_l*(max_sat/255)/palette_l # her's finally the palette...

# first of all make an empy plot
plot(NULL, xlim=rangepropCA, ylim=rangepropCB, log='', xaxt='n', yaxt='n', xlab='prop A', ylab='prop B', bty='n', main='color field');
# then fill it up with rectangles each colored differently
for (m in 1:palette_l) {
    for (n in 1:palette_l) {
        rgbcol<-rgb(colorpalette[n],colorpalette[m],0, alpha_default);
        rect(xleft= prop(x=(n-1)/(palette_l),rangepropCA[1],rangepropCA[2]) 
            ,xright= prop(x=(n)/(palette_l),rangepropCA[1],rangepropCA[2])
            ,ytop= prop(x=(m-1)/(palette_l),rangepropCB[1],rangepropCB[2]) 
            ,ybottom= prop(x=(m)/(palette_l),rangepropCB[1],rangepropCB[2])
            ,col=rgbcol
            ,border="transparent"
        )
    }
}
# done!

这篇关于2d中的颜色梯度图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆