r - R中水平图的颜色

标签 r plot lattice levelplot

我有以下代码。它会生成一个水平图,其中小于 0 的正方形值应以红色色调着色,而值大于 0 的正方形应以蓝色色调着色。然后我希望值为 0 的正方形被涂成白色。然而,没有什么最终是白色的。我该如何解决这个问题?

第一列中的所有三个方 block 都应该是白色的。

library(lattice)

cc = colorRampPalette( c("red", "white","blue"))
trellis.par.set(regions=list(col=cc(20)))
x = c(1,2,3,1,2,3,1,2,3)
y = c(1,1,1,2,2,2,3,3,3)
z = c(0,-2,-3,0,2,3,0,1,-1)
df = data.frame(x,y,z)
p <- levelplot(z~x*y, df,
               panel=function(...) {
                 arg <- list(...)
                 panel.levelplot(...)
                 })
print(p)

enter image description here


更新: 这是一个可重现的示例,它试图修复它,但仍然不太正确: 这是一个dataframe df:

    x y           z
1   1 1 -0.17457167
2   2 1  0.93407856
3   3 1  0.55129545
4   4 1  0.97388216
5   5 1 -1.00000000
6   6 1  0.52883410
7   7 1 -1.00000000
8   8 1  0.85112829
9   9 1 -1.00000000
10 10 1  1.00000000
11 11 1 -0.87714166
12 12 1  1.00000000
13 13 1 -0.95403260
14 14 1  1.00000000
15 15 1 -0.91600501
16 16 1  1.00000000
17 17 1 -1.00000000
18 18 1 -0.38800669
19 19 1 -0.52110322
20 20 1  0.00000000
21 21 1 -0.08211450
22 22 1  0.55390723
23 23 1  1.00000000
24 24 1 -0.04147514
25 25 1 -1.00000000
26 26 1 -0.39751358
27 27 1 -0.99550773
28 28 1  0.00000000
29 29 1  0.20737568
30 30 1  0.00000000
31 31 1  0.00000000
32 32 1  0.00000000
33 33 1 -0.26702883

然后是代码:

cc = colorRampPalette( c("red", "white","blue"))
trellis.par.set(regions=list(col=cc(21)))
zrng <- range(z)       # what's the range of z
tol <- 1e-2            # what tolerance is necessary?
colorBreaks <- c(
  seq(zrng[1] - 0.01, 0 - tol, length.out = 11),
  seq(0 + tol,zrng[2] + 0.01,length.out = 10))  
p <- levelplot(z~x*y, df, 
               at = colorBreaks,
               panel=function(...) {
                 arg <- list(...)
                 panel.levelplot(...)
               })
print(p)

它生成此图,该图在光谱中没有白色槽: enter image description here

最佳答案

正如 thelatemail 所指出的,cc(20) 永远不会产生白色 ("#FFFFFF")。您必须使用奇数来准确表示色带的中间值(检查 cc(3)cc(4))。

现在,您需要为 levelplot 设置 at 参数以设置颜色的断点。默认是 at = pretty(z):

#[1] -3 -2 -1  0  1  2  3

但您不希望 0 成为断点。您希望它有自己的颜色,并与色带的中间对齐。

您可以通过将断点设置为尽可能接近 0(在某些 tol 内)来实现这一点,以防止任何其他值映射到白色。粗略的想法是通过做这样的事情为 0 留一个小点 at = c(seq(-3.01, -0.00001, length.out = 11), seq(0.00001, 3.01, length.out = 11) ) 或使用如下所示的类似方法。因为色带具有奇数个值,所以序列需要偶数个值。 (即 3 种颜色的色带可以除以 2 个断点,但 4 个值的色带可以除以 3 个断点)

trellis.par.set(regions=list(col=cc(21)))

# Define a sequence of breaks for the at argument to levelplot.
zrng <- range(z)       # what's the range of z
tol <- 1e-5            # what tolerance is necessary?
colorBreaks <- c(
  seq(zrng[1] - 0.01,  # adding a small buffer on end
      0 - tol,
      length.out = 11),
  seq(0 + tol,
      zrng[2] + 0.01,
      length.out = 11))  
      # note, I chose length.out = 11.
      # Don't do more than roughly ceiling((# of colors) / 2) 


p <- levelplot(z~x*y, df, 
               at = colorBreaks,
               panel=function(...) {
                 arg <- list(...)
                 panel.levelplot(...)
                 })

enter image description here

关于r - R中水平图的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40274265/

相关文章:

matlab - 在matlab中寻找瀑布图的变体

r - 在函数内保存 mirt 的绘图

r - 使用R中数据框中的多列创建列联表

r - R中重叠的唯一数据帧

r - 如何在R中的某些特定区域填充颜色?

r - 重新排序因子后正确绘制 xyplot 线

r - 在 R 中,错误 "need at least one panel"是什么意思,如何解决?

r - 如何折叠具有相同标识符的行并保留非空列值?

r - 计算数据帧 R 中每行的值

r - 在 R 中,如何随时间(3D 或动画)绘制 x,y 值?