Colourscale:改变过渡开始的地方

时间:2017-05-14 09:31:26

标签: r colors heatmap lattice

我使用levelplot包中的lattice函数绘制热图。我的矩阵中的值范围从-1到1,我使用以下比例:

cols <- colorRampPalette(c("blue", "white", "red"))(256)

一个小例子:

d <- 20
df <- expand.grid(x = 1:d, y = 1:d)
df$z <- runif(d*d, -1, 1)

levelplot(z ~ x * y, data = df,
          col.regions = cols)

enter image description here

我希望调整比例,以便更大比例的比例为白色。我假设我必须手动定义转换开始的位置或更好地定义不同的“转换速度” - 当我接近0(即白色)时较慢,而当我接近1或-1时更快。

任何有关如何做到这一点的建议将不胜感激!

1 个答案:

答案 0 :(得分:0)

您可以将插值切换为spline以获得更平滑的过渡。

cols <- colorRampPalette(c("blue", "white", "red"), interpolate = "spline")(256)

enter image description here

你也可以使用来自RColorBrewer的更好的调色板

cols2 <- colorRampPalette(RColorBrewer::brewer.pal(11, "RdBu"))(256)
levelplot(z ~ x * y, data = df, col.regions = rev(cols2))

enter image description here