R中心调色板在0 [英] R center color palette on 0

查看:107
本文介绍了R中心调色板在0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个以0为中心的色带。

I would like to create a color ramp centered on 0.

以下代码:

library(raster)
librayr(RColorBrewer)

xmin = -124.413
xmax = -66.883
ymin = 25.9425
ymax = 48.9885
nrows = 215
ncols = 254

empty_raster = raster(xmn=xmin, ymn=ymin, nrows = nrows, ncols = ncols)
zscores_coords = cbind(seq(from=-124.413, to=-66.883, length.out=1212), 
                       seq(from=25.9425, to=48.9885, length.out=1212))
zscores_raster = rasterize(zscores_coords, empty_raster, field = 1)
x_coord_raster = rasterize(zscores_coords, zscores_raster, 
                           field=zscores_coords[,1])
y_coord_raster = rasterize(zscores_coords, zscores_raster, 
                           field=zscores_coords[,2])

BUD_zscores_coordinates = 
  data.frame(x = zscores_coords[,1], y = zscores_coords[,2], 
             BUD_zscores = seq(from=-20.51558, to=14.34510, length.out=1212))
coordinates(BUD_zscores_coordinates) = ~x+y
zscores_pres = rasterize(BUD_zscores_coordinates, zscores_raster, field=1)
zscores_points = rasterToPoints(zscores_pres)
zscores_points = zscores_points[,c(1:2)]

zscores_nodes = rasterize(BUD_zscores_coordinates, empty_raster, 
                          field = BUD_zscores_coordinates$BUD_zscores,
                          fun = mean)

#Plot! 
colorramp = brewer.pal(11,"RdBu")
applycolors = colorRampPalette(colorramp)
plot(zscores_nodes, xlim = c(xmin, xmax), ylim = c(ymin, ymax), 
     col = applycolors(100))

产生以下图形:

其中 0位于色带的浅蓝色部分。关于如何使其与白色部分对齐的任何建议?我一直在努力使用中断而无济于事。

In which '0' is in the light blue section of the color ramp. Any advice on how to make it align with the white section? I've struggled with using breaks to no avail.

推荐答案

使用此答案: https://stackoverflow.com/a/10986203/4632634 ,我能够解决自己的问题。

Using this answer: https://stackoverflow.com/a/10986203/4632634, I was able to get what I needed for my problem.

library(raster)
library(RColorBrewer)

xmin = -124.413
xmax = -66.883
ymin = 25.9425
ymax = 48.9885
nrows = 215
ncols = 254

empty_raster = raster(xmn=xmin, ymn=ymin, nrows = nrows, ncols = ncols)
zscores_coords = cbind(seq(from=-124.413, to=-66.883, length.out=1212), 
                       seq(from=25.9425, to=48.9885, length.out=1212))
zscores_raster = rasterize(zscores_coords, empty_raster, field = 1)
x_coord_raster = rasterize(zscores_coords, zscores_raster, 
                           field=zscores_coords[,1])
y_coord_raster = rasterize(zscores_coords, zscores_raster, 
                           field=zscores_coords[,2])

BUD_zscores_coordinates = 
  data.frame(x = zscores_coords[,1], y = zscores_coords[,2], 
             BUD_zscores = seq(from=-20.51558, to=14.34510, length.out=1212))
coordinates(BUD_zscores_coordinates) = ~x+y
zscores_pres = rasterize(BUD_zscores_coordinates, zscores_raster, field=1)
zscores_points = rasterToPoints(zscores_pres)
zscores_points = zscores_points[,c(1:2)]

zscores_nodes = rasterize(BUD_zscores_coordinates, empty_raster, 
                          field = BUD_zscores_coordinates$BUD_zscores,
                          fun = mean)

zscores_stack = stack(zscores_nodes, zscores_raster, x_coord_raster, 
                      y_coord_raster)
zscores_table = extract(zscores_stack, zscores_points)

#Plot! (bluer = flyway zscores > eBird szcores; red = eBird zscores > 
#flyway zscores)
nHalf = nrow(zscores_table)/2
Min = min(zscores_table[,1])
Max = max(zscores_table[,1])
Thresh = 0

## Make vector of colors for values below threshold
rc1 = colorRampPalette(colors = c("red", "white"), space="Lab")(nHalf)    
## Make vector of colors for values above threshold
rc2 = colorRampPalette(colors = c("white", "blue"), space="Lab")(nHalf)
rampcols = c(rc1, rc2)
## In your example, this line sets the color for values between 49 and 51. 
rampcols[c(nHalf, nHalf+1)] = rgb(t(col2rgb("white")), maxColorValue=256) 

rb1 = seq(Min, Thresh, length.out=nHalf+1)
rb2 = seq(Thresh, Max, length.out=nHalf+1)[-1]
rampbreaks = c(rb1, rb2)

r.range = c(Min, Max)
plot(zscores_nodes, xlim = c(xmin, xmax), ylim = c(ymin, ymax), 
     col = rampcols, breaks=rampbreaks, legend.width = 1, legend.shrink = 0.75, 
     axis.args=list(at=c(-20, 0, 14), labels=c(-20, 0, 14), 
                    cex.axis=0.6), 
     legend.args=list(text='Z-Score', side=4, font=2, line=2.5, cex=0.8))

收益:

0很好地位于白色区域的中心。

Where 0 is nicely centered on the white zone.

这篇关于R中心调色板在0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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