来自自定义 colorRampPalette 的颜色条 [英] Colorbar from custom colorRampPalette

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

问题描述

我已经定义了一个 colorRampPalette:

I have defined a colorRampPalette:

my.colors = colorRampPalette(c("light green", "yellow", "orange", "red"))

如何为它绘制颜色条图例"项目,最好仅使用基本包?我正在寻找一个填充了该颜色渐变的矩形.

How can I plot a colorbar "legend" item for it, preferably using only the base packages? I am after a rectangle filled with that color gradient.

我真正想要的是一种生成用光栅"光栅绘制的相同类型的图例(颜色条)的方法:

What I am really after is a way to produce the same type of legend (color bar) that is plotted with a "raster" raster:

require(raster)
plot(raster("myfile.tif"), legend=T)

我需要能够将它放在另一个图的顶部.

I need to be able to place this on top of another plot.

推荐答案

我前段时间做了一个很好的灵活函数来做到这一点.

I made a nice flexible function awhile ago to do this.

# Function to plot color bar
color.bar <- function(lut, min, max=-min, nticks=11, ticks=seq(min, max, len=nticks), title='') {
    scale = (length(lut)-1)/(max-min)

    dev.new(width=1.75, height=5)
    plot(c(0,10), c(min,max), type='n', bty='n', xaxt='n', xlab='', yaxt='n', ylab='', main=title)
    axis(2, ticks, las=1)
    for (i in 1:(length(lut)-1)) {
     y = (i-1)/scale + min
     rect(0,y,10,y+1/scale, col=lut[i], border=NA)
    }
}

然后您可以执行以下操作:

Then you can do something like:

> color.bar(colorRampPalette(c("light green", "yellow", "orange", "red"))(100), -1)

更多示例位于:http://www.colbyimaging.com/wiki/statistics/色条

这篇关于来自自定义 colorRampPalette 的颜色条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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