如何控制 scale_fill_continuous_divergingx 的不平衡色标,其中一端应为对数?

How do I control an unbalanced color scale with scale_fill_continuous_divergingx where one end should be logarithmic?

我的问题与相似,但不相同。

我想对 p 值应用发散色标,其中值 0.05 是我的中点。高于 0.05 的值应线性增长,而低于 0.05 的值应呈对数增长。 colorspace::scale_fill_continuous_divergingxcolorspace 包中的其他功能是否可行?

或者,我可以转换为对数刻度,但我很难以视觉上有意义的方式将偏移标记为 0.05 以上。

您可以在下面找到我到目前为止尝试过的内容。非常欢迎任何想法。

df <- structure(list(name = c(3L, 12L, 15L, 14L, 5L, 18L, 11L, 4L, 
6L, 17L, 10L, 2L, 9L, 8L, 7L, 1L, 16L, 19L, 13L, 9L, 2L, 8L, 
15L, 16L, 17L, 4L, 19L, 10L, 7L, 1L, 6L, 5L, 11L, 12L), p_adjusted = c(4.32596750954342e-06, 
3.03135847907459e-05, 0.000118088275490085, 0.000131741744620688, 
0.000137720927111689, 0.00427368416054269, 0.00435924240679527, 
0.0105749752039341, 0.0108537078105272, 0.0156289799697254, 0.823419406127695, 
1, 1, 1, 1, 1, 1, 1, 3.57724493033791e-06, 9.05031572894023e-05, 
0.000118883184319132, 0.000143702004459057, 0.00033101896024948, 
0.00265474345049394, 0.00453440320908698, 0.00473248203895472, 
0.00508912585948996, 0.00881057444851548, 0.0200752446003521, 
0.024238863465647, 1, 1, 1, 1), group = c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L)), row.names = c(NA, 
-34L), class = c("tbl_df", "tbl", "data.frame"))

这或多或少使用了默认比例。色标提供了高于 0.05 时发生的情况的不错图片。

ggplot2::ggplot(df, ggplot2::aes(x = group, y = name, fill = p_adjusted)) +
  ggplot2::geom_tile() +
  colorspace::scale_fill_continuous_divergingx(name = "p-value",
                                               mid = 0.05,
                                               palette = "RdYlBu") +
  ggplot2::theme_classic() +
  ggplot2::scale_x_discrete("Group") +
  ggplot2::scale_y_discrete("Feature")

然后我尝试在 0.05 以下实现更快的比例进展,以便在那里获得更多不同的颜色。这或多或少失败了。

ggplot2::ggplot(df, ggplot2::aes(x = group, y = name, fill = p_adjusted)) +
  ggplot2::geom_tile() +
  colorspace::scale_fill_continuous_divergingx(
    name = "p-value",
    mid = 0.05,
    palette = "RdYlBu",
    p1 = 1e-2,
    p2 = 1e-2,
    p3 = 1.5,
    p4 = 1.5
  ) +
  ggplot2::theme_classic() +
  ggplot2::scale_x_discrete("Group") +
  ggplot2::scale_y_discrete("Feature")

最后,我可以使用对数转换比例,它清楚地显示低于 0.05 的不同幅度,但无法区分高于 0.05 的幅度。另外,我想清楚地区分colorbar上的中点0.05。

ggplot2::ggplot(df, ggplot2::aes(x = group, y = name, fill = p_adjusted)) +
  ggplot2::geom_tile() +
  colorspace::scale_fill_continuous_divergingx(
    name = "p-value",
    mid = 0.05,
    palette = "RdYlBu",
    labels = function(x)
      format(x, digits = 3, big.mark = ","),
    trans = "log"
  ) +
  ggplot2::theme_classic() +
  ggplot2::scale_x_discrete("Group") +
  ggplot2::scale_y_discrete("Feature")

你对数变换尺度的想法是正确的。唯一的问题是 there is a bug 其中中点没有得到转换。所以,通过预先转换中点值,我们应该得到一个以中点为中心的发散尺度。

library(ggplot2)

df <- structure(list(
  name = c(3L, 12L, 15L, 14L, 5L, 18L, 11L, 4L, 6L, 17L, 10L, 2L, 9L, 8L, 7L, 
           1L, 16L, 19L, 13L, 9L, 2L, 8L, 15L, 16L, 17L, 4L, 19L, 10L, 7L, 1L, 
           6L, 5L, 11L, 12L), 
  p_adjusted = c(4.32596750954342e-06, 3.03135847907459e-05, 
                 0.000118088275490085, 0.000131741744620688,
                 0.000137720927111689, 0.00427368416054269, 
                 0.00435924240679527, 0.0105749752039341, 0.0108537078105272, 
                 0.0156289799697254, 0.823419406127695, 1, 1, 1, 1, 1, 1, 1, 
                 3.57724493033791e-06, 9.05031572894023e-05,
                 0.000118883184319132, 0.000143702004459057, 
                 0.00033101896024948, 0.00265474345049394, 0.00453440320908698, 
                 0.00473248203895472, 0.00508912585948996, 0.00881057444851548, 
                 0.0200752446003521, 0.024238863465647, 1, 1, 1, 1), 
  group = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
            1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L)
  ), row.names = c(NA, -34L), class = c("tbl_df", "tbl", "data.frame"))

ggplot2::ggplot(df, ggplot2::aes(x = group, y = name, fill = p_adjusted)) +
  ggplot2::geom_tile() +
  colorspace::scale_fill_continuous_divergingx(
    trans = "log10",
    name = "p-value",
    mid = log10(0.05),
    palette = "RdYlBu"
  ) +
  ggplot2::theme_classic() +
  ggplot2::scale_x_discrete("Group") +
  ggplot2::scale_y_discrete("Feature")

reprex package (v1.0.0)

于 2021-03-30 创建