在 R 中,我有一堆数据大约为 0,一些数据大约为 1,我想“重新调整”我的热色以区分较低的数字。这必须以彩虹方式完成,我不想“离散颜色”。我尝试在 image.plot 中打断,但它不起作用。
image.plot(X,Y,as.matrix(mymatrix),col=heat.colors(800),asp=1,scale="none")
我试过了:
lowerbreak=seq(min(values),quantile2,len=80)
highbreak=seq(quantile2+0.0000000001,max(values),len=20)
break=c(lowerbreak,highbreak)
ii <- cut(values, breaks = break,
include.lowest = TRUE)
colors <- colorRampPalette(c("lightblue", "blue"))(99)[ii]
最佳答案
这是一种使用“squash”库的方法。使用 makecmap()
,您可以指定颜色值和中断,还可以使用 base
参数指定它应该被对数拉伸(stretch)。它有点复杂,但可以让您进行精细控制。我用它来为偏斜数据着色,我需要在“低端”进行更多定义。
为了实现彩虹调色板,我使用了内置的“jet”颜色功能,但您可以使用任何颜色集 - 我举了一个使用“colorRampPalette”创建灰度渐变的示例。
无论您使用什么斜坡,都需要使用 base
值来优化您的数据。
install.packages("squash")
library("squash")
#choose your colour thresholds - outliers will be RED
minval=0 #lowest value to get a colour
maxval=2.0 #highest value to get a colour
n.cols=100 #how many colours do you want in your palette?
col.int=1/n.cols
#create your palette
colramp=makecmap(x=seq(minval,maxval,col.int),
n=n.cols,
breaks=prettyLog,
symm=F,
base=10,#to give ramp a log(base) stretch
colFn=jet,
col.na="red",
right=F,
include.lowest=T)
# If you don't like the colFn options in "makecmap", define your own!
# Here's an example in greyscale; pass this to "colFn" above
user.colfn=colorRampPalette(c("black","white"))
在绘图中使用 colramp 的示例(假设您已经在程序中的某处创建了 colramp):
varx=1:100
vary=1:100
plot(x,y,col=colramp$colors) #colors is the 2nd vector in the colramp list
要选择特定的颜色,通过例如颜色[1:20] 从列表中选择特定的颜色(如果您在上面的示例中尝试这样做,第一个颜色将重复 5 次 - 不是很有用,但您明白了逻辑并且可以玩玩)。
在我的例子中,我有一个值网格,我想将其转换为彩色光栅图像(即对一些连续数据进行颜色映射)。这是使用组成矩阵的示例代码:
#create a "dummy matrix"
matx=matrix(data=c(rep(2,50),rep(0,500),rep(0.5,500),rep(1,500),rep(1.5,500)),nrow=50,ncol=41,byrow=F)
#transpose the matrix
# the output of "savemat" is rotated 90 degrees to the left
# so savemat(maty) will be a colorized version of (matx)
maty=t(matx)
#savemat creates an image using colramp
savemat(x=maty,
filename="/Users/KeeganSmith/Desktop/matx.png",
map=colramp,
outlier="red",
dev="png",
do.dev.off=T)
关于在 r 中重新缩放调色板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46727749/