本文介绍了设置中点以在热图上连续发散色标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过ggplot2调整热图的中点位置.我四处搜寻,发现scale_fill_gradient2非常适合,但颜色似乎与我要寻找的不匹配.我知道z需要从0到1的范围.下面的示例数据集生成:

I need to adjust the midpoint location for a heatmap via ggplot2. I've googled around and have seen scale_fill_gradient2 be a great fit but the colors don't seem to match up to what I'm looking for. I know z needs a range from 0 to 1. Example dataset generation below:

library(ggplot2)
library(tibble)
library(RColorBrewer)

set.seed(5)
df <- as_tibble(expand.grid(x = -5:5, y = 0:5, z = NA))
df$z <- runif(length(df$z), min = 0, max = 1)

我尝试使用scale_fill_gradient2进行绘图,但是蓝色并不像我想要的那样暗".

I tried plotting with the scale_fill_gradient2 but the blue color isn't coming as "dark" as I'd like.

ggplot(df, aes(x = x, y = y)) +
  geom_tile(aes(fill = z)) +
  scale_fill_gradient2(
    low = 'red', mid = 'white', high = 'blue',
    midpoint = 0.7, guide = 'colourbar', aesthetics = 'fill'
  ) +
  scale_x_continuous(expand = c(0, 0), breaks = unique(df$x)) +
  scale_y_continuous(expand = c(0, 0), breaks = unique(df$y))

因此,我将scale_fill_distiller与调色板"RdBu"一起使用,该调色板带有我需要的色标,但范围和中点不正确.

Therefore, I'm using scale_fill_distiller with the color palette 'RdBu' which comes out with the color scale I need but the ranges and the midpoints aren't right.

ggplot(df, aes(x = x, y = y)) +
  geom_tile(aes(fill = z)) +
  scale_fill_distiller(palette = 'RdBu') +
  scale_x_continuous(expand = c(0, 0), breaks = unique(df$x)) +
  scale_y_continuous(expand = c(0, 0), breaks = unique(df$y))

是否有办法获得第二个色标,但可以选择将中点范围设置为第一个?

Is there a way to get the 2nd color scale but with the option to set midpoint range as the first?

推荐答案

colorspace软件包提供的色阶通常可以使您进行更细粒度的控制.首先,您可以使用相同的色标,但设置中点.

The color scales provided by the colorspace package will generally allow you much more fine-grained control. First, you can use the same colorscale but set the mid-point.

library(ggplot2)
library(tibble)
library(colorspace)

set.seed(5)
df <- as_tibble(expand.grid(x = -5:5, y = 0:5, z = NA))
df$z <- runif(length(df$z), min = 0, max = 1)

ggplot(df, aes(x = x, y = y)) +
  geom_tile(aes(fill = z)) +
  scale_fill_continuous_divergingx(palette = 'RdBu', mid = 0.7) +
  scale_x_continuous(expand = c(0, 0), breaks = unique(df$x)) +
  scale_y_continuous(expand = c(0, 0), breaks = unique(df$y))

但是,正如您所看到的,这会产生与以前相同的问题,因为您必须离中点较远才能获得更暗的蓝调.幸运的是,发散度色标允许您独立地操纵任何一个分支,因此我们可以创建一个标度,该色标可以更快地变为深蓝色.您可以玩l3p3p4直到获得所需的结果.

However, as you see, this creates the same problem as before, because you'd have to be further away from the midpoint to get darker blues. Fortunately, the divergingx color scales allow you to manipulate either branch independently, and so we can create a scale that turns to dark blue much faster. You can play around with l3, p3, and p4 until you get the result you want.

ggplot(df, aes(x = x, y = y)) +
  geom_tile(aes(fill = z)) +
  scale_fill_continuous_divergingx(palette = 'RdBu', mid = 0.7, l3 = 0, p3 = .8, p4 = .6) +
  scale_x_continuous(expand = c(0, 0), breaks = unique(df$x)) +
  scale_y_continuous(expand = c(0, 0), breaks = unique(df$y))

这篇关于设置中点以在热图上连续发散色标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 04:33