了解为scale_fill_continuous_divergingx输入的参数以处理颜色边距

时间:2019-11-07 07:39:14

标签: r ggplot2 heatmap

该问题是我上一个问题here的继续。

我有一个带有可用数据集的热图。数据集粘贴在下面:

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, l3 = 54, p3 = 2206, p4 = 325)

我从图形中期望的是,白色将以某个特定值为中心,而其他渐变将基于该值而高于或低于该值。但是,通过使用不同的参数,似乎我无法完全理解参数l3p3p4的含义。当我查看此功能的documentation时,它建议自定义比例的参数来自colorspace包中的divergingx_hcl函数。

在查看divergingx_hcl documentation时,指出它们与不同的输入参数相对应。我完全迷路了,完全不知道这是什么。任何有关帮助我围绕这些参数(不仅是l3p3p4,还包括其他参数)的指导都将不胜感激。

reprex package(v0.3.0)于2019-11-07创建

1 个答案:

答案 0 :(得分:1)

首先,将所有颜色都指定为HCL(色相,色度,亮度),其对应于颜色的类型(红色,绿色,蓝色等),颜色的鲜艳程度(低色度为灰色,高色度)颜色非常鲜艳),以及颜色的亮度(高亮度是白色,低亮度是黑色)。

参数l3表示色标一端的颜色亮度分量。 (l1是另一端的亮度,l2是中间的亮度。)亮度从0到100。因此,如果希望末端的颜色更暗,请设置亮度降低。参数p3p4是控制颜色从中点过渡到端点的速度的指数。通常,接近0的值表示过渡快,而大于1的值则过渡慢。您不太可能希望p3p4的值大于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

reprex package(v0.3.0)于2019-11-07创建

这表明l3指定的终点颜色已经很暗。将l3从15更改为0会使它变暗一些,但幅度不大。此外,未指定p2p3p4,这意味着它们全部取自p1,因此为1.4。因此,颜色插值比线性插值要慢一些。

有了这些知识,以下示例应该是有意义的。要了解更多信息,我建议阅读colorspace网站上的各种文章: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
  )

reprex package(v0.3.0)于2019-11-07创建