首先,所有的颜色都被指定为HCL(色调、彩度、亮度),分别对应颜色的类型(红、绿、蓝等)、颜色的鲜艳度(低彩度是灰色,高彩度是非常鲜艳的) ),以及颜色的亮度(高亮度为白色,低亮度为黑色)。
参数l3
表示色阶一端颜色的亮度分量。 (l1
是另一端的亮度,并且l2
是中间的亮度。)亮度从 0 到 100。因此,如果您希望末端的颜色更暗,请将亮度设置为较低的值。参数p3
and p4
是控制颜色从中点过渡到端点的速度的指数。一般来说,接近 0 的值意味着更快的转换,大于 1 的值意味着更慢的转换。你不太可能想要p3
or p4
值大于 10。
要获取调色板的默认参数,您可以使用divergingx_palettes()
命令:
library(colorspace)
divergingx_palettes('RdBu')
#> HCL palette
#> Name: RdBu
#> Type: Diverging (flexible)
#> Parameter ranges:
#> h1 h2 h3 c1 c2 c3 l1 l2 l3 p1
#> 20 NA 230 60 0 50 20 98 15 1.4
Created on 2019-11-07 by the reprex package https://reprex.tidyverse.org (v0.3.0)
这表明由指定的终点处的颜色l3
天已经很黑了。改变l3
从 15 到 0 会使颜色变暗一点,但不会太多。更远,p2
, p3
, and p4
没有指定,这意味着它们都取自p1
,因此是 1.4。因此,颜色插值比线性插值稍慢。
有了这些知识,下面的例子就应该有意义了。要了解更多信息,我建议阅读色彩空间网站上的各种文章:http://colorspace.r-forge.r-project.org/ http://colorspace.r-forge.r-project.org/
首先是数据:
library(ggplot2)
library(colorspace)
bigtest <- structure(list(x = c(-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8,
-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, -8,
-7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, -8, -7,
-6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, -8, -7, -6,
-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, -8, -7, -6, -5,
-4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8),
y = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5),
z = c(1281.35043, 576.76381, 403.46607,
363.28815, 363.13356, 335.04997, 246.93314, 191.56371, 165.35087,
165.35087, 136.33712, 83.91203, 107.5773, 56.91087, 56.91089,
54.16559, 54.18172, 1841.60838, 1098.66304, 424.80686, 363.52776,
363.13355, 335.04998, 246.93314, 191.69473, 165.35087, 165.35087,
136.33712, 83.91204, 107.57729, 56.91087, 56.91088, 54.16421,
54.16794, 2012.52217, 1154.7927, 446.79023, 363.31379, 363.13356,
335.04997, 246.93314, 191.9613, 165.35087, 165.35087, 136.33712,
83.91202, 107.57731, 56.91088, 56.91088, 54.1642, 54.16559, 2077.10354,
1217.43403, 450.18301, 363.44225, 363.13357, 363.13363, 253.99753,
218.43223, 165.35087, 165.35014, 136.33712, 83.91203, 107.57822,
82.87399, 56.91087, 54.1642, 54.1642, 2092.56391, 1229.49925,
451.15179, 392.30728, 363.13356, 363.13282, 264.18944, 218.4308,
165.35087, 165.35044, 136.33712, 83.91202, 83.92709, 82.87353,
82.87406, 56.54491, 54.16421, 2206.93318, 1231.66411, 457.37767,
392.41558, 363.13357, 363.13283, 335.06272, 191.95211, 165.35087,
165.35014, 136.33712, 136.35211, 112.12755, 82.73634, 82.87353,
82.87418, 54.16421)),
row.names = c(NA, -102L),
class = c("tbl_df", "tbl", "data.frame"))
现在的情节:
ggplot(bigtest, aes(x = x, y = y)) +
geom_tile(aes(fill = z)) +
scale_fill_continuous_divergingx(
palette = 'RdBu', rev = TRUE,
mid = 347.48
)
ggplot(bigtest, aes(x = x, y = y)) +
geom_tile(aes(fill = z)) +
scale_fill_continuous_divergingx(
palette = 'RdBu', rev = TRUE,
mid = 347.48,
p3 = .2,
p4 = .2
)
ggplot(bigtest, aes(x = x, y = y)) +
geom_tile(aes(fill = z)) +
scale_fill_continuous_divergingx(
palette = 'RdBu', rev = TRUE,
mid = 347.48,
l3 = 0,
p3 = .2,
p4 = .2
)
Created on 2019-11-07 by the reprex package https://reprex.tidyverse.org (v0.3.0)