在R我有一个零左右的数据云,一些数据在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]
这是使用“壁球”库的方法。使用makecmap()
,您可以指定颜色值和中断,还可以指定应使用base
参数进行日志拉伸。它有点复杂,但可以为您提供精细控制。我使用它来为偏斜数据着色,我需要在“低端”中进行更多定义。
为了实现彩虹调色板,我使用了内置的“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)
使用colorRampPalette
时,可以设置bias
参数以强调低(或高)值。
像colorRampPalette(heat.colors(100),bias=3)
这样的东西会使“斜坡”聚焦在较低的位置,帮助它们在视觉上更加明显。