如何在 ggplot 中设置具有固定中点的渐变色标?

huangapple go评论61阅读模式
英文:

How do I set a gradient color scale with a fixed midpoint with ggplot?

问题

我想制作一个带有自定义填充颜色比例的条形图。假设数据既有z_score的正值也有负值:

data_1 <- data.frame(
  name = paste('Pathway', 1:5),
  p_val = c(0.01, 0.05, 0.03, 0.001, 0.02),
  z_score = c(-2, -3, 3, 4, 2.5)
)

data_1 %>%
  ggplot(aes(x = p_val, y = fct_reorder(name, p_val))) +
  geom_bar(aes(fill = z_score), stat = 'identity') +
  scale_fill_gradientn(
    colours = colorRampPalette(c('#709AE1', '#FFFFFF', '#FD7446'))(100)
  )

如何在 ggplot 中设置具有固定中点的渐变色标?

以上代码按预期工作。现在,假设有一个只有负z_score的数据:

data_2 <- data.frame(
  name = paste('Pathway', 1:5),
  p_val = c(0.01, 0.05, 0.03, 0.001, 0.02),
  z_score = c(-2, -3, -3, -4, -2.5)
)
data_2 %>%
  ggplot(aes(x = p_val, y = fct_reorder(name, p_val))) +
  geom_bar(aes(fill = z_score), stat = 'identity') +
  scale_fill_gradientn(
    colours = colorRampPalette(c('#709AE1', '#FFFFFF', '#FD7446'))(100)
  )

这会产生错误的图,因为我希望颜色从白色映射到蓝色(所有z_score值都为负):

如何在 ggplot 中设置具有固定中点的渐变色标?

我尝试更改填充颜色,以便在所有z_score值都为负的情况下只有白色和蓝色,但这会产生不正确的图:

如何在 ggplot 中设置具有固定中点的渐变色标?

对于仅有负值的z_score,我希望白色仅用于接近零的值。以某种方式,比例应该固定。例如,z_score为-2不应映射为白色,而应该仍然是蓝色,即使它是最小的值。这有道理吗?

感激任何提示!

英文:

I want to make a bar plot with the custom fill color scale. Suppose the following, the data has both positive and negative values for z_score:

data_1 &lt;- data.frame(
  name = paste(&#39;Pathway&#39;, 1:5),
  p_val = c(0.01, 0.05, 0.03, 0.001, 0.02),
  z_score = c(-2, -3, 3, 4, 2.5)
)

data_1 |&gt;
  ggplot(aes(x = p_val, y = fct_reorder(name, p_val))) +
  geom_bar(aes(fill = z_score), stat = &#39;identity&#39;) +
  scale_fill_gradientn(
    colours = colorRampPalette(c(&#39;#709AE1&#39;, &#39;#FFFFFF&#39;, &#39;#FD7446&#39;))(100)
  )

如何在 ggplot 中设置具有固定中点的渐变色标?

The code above works as expected. Now, suppose a data with only negative z_score:

data_2 &lt;- data.frame(
  name = paste(&#39;Pathway&#39;, 1:5),
  p_val = c(0.01, 0.05, 0.03, 0.001, 0.02),
  z_score = c(-2, -3, -3, -4, -2.5)
)
data_2 |&gt;
  ggplot(aes(x = p_val, y = fct_reorder(name, p_val))) +
  geom_bar(aes(fill = z_score), stat = &#39;identity&#39;) +
  scale_fill_gradientn(
    colours = colorRampPalette(c(&#39;#709AE1&#39;, &#39;#FFFFFF&#39;, &#39;#FD7446&#39;))(100)
  )

This produces incorrect plot, since I want colors to map from white to blue (all z_score values are negative):

如何在 ggplot 中设置具有固定中点的渐变色标?

I tried changing the fill color so that there's only white and blue colors in case all z_score values are negative, but this gives an incorrect plot:
如何在 ggplot 中设置具有固定中点的渐变色标?

For negative-only z_score, I want a white color to be used only for values close to zero. Somehow, the scale should be fixed. For example, a z_score of -2 shouldn't map to white, but should still be blue even though it's the smallest value. Does this make sense?

Appreciate any tips!

答案1

得分: 1

看看 scale_fill_gradient2 函数是否达到你的目标。你可以为 lowhighmid 值设置颜色。默认情况下,中点设置为零,并使用白色。

使用此函数,无论值是负数还是正数,都可以使用相同的填充比例。

library(tidyverse)

data_1 |&gt;
  ggplot(aes(x = p_val, y = fct_reorder(name, p_val))) +
  geom_bar(aes(fill = z_score), stat = 'identity') +
  scale_fill_gradient2(low = '#709AE1', high = '#FD7446')

如何在 ggplot 中设置具有固定中点的渐变色标?

data_2 |&gt;
  ggplot(aes(x = p_val, y = fct_reorder(name, p_val))) +
  geom_bar(aes(fill = z_score), stat = 'identity') +
  scale_fill_gradient2(low = '#709AE1', high = '#FD7446')

如何在 ggplot 中设置具有固定中点的渐变色标?

英文:

See if the scale_fill_gradient2 function achieves your goal. You can set colours for low, high and mid values. The mid-point is set to zero with a white colour by default.

With this function, you can use the same fill scale no matter the values are negative or positive.

library(tidyverse)

data_1 |&gt;
  ggplot(aes(x = p_val, y = fct_reorder(name, p_val))) +
  geom_bar(aes(fill = z_score), stat = &#39;identity&#39;) +
  scale_fill_gradient2(low = &#39;#709AE1&#39;, high = &#39;#FD7446&#39;)

如何在 ggplot 中设置具有固定中点的渐变色标?

data_2 |&gt;
  ggplot(aes(x = p_val, y = fct_reorder(name, p_val))) +
  geom_bar(aes(fill = z_score), stat = &#39;identity&#39;) +
  scale_fill_gradient2(low = &#39;#709AE1&#39;, high = &#39;#FD7446&#39;)

如何在 ggplot 中设置具有固定中点的渐变色标?

答案2

得分: 0

我们可以使用scale_fill_gradientn函数的breakslimits参数进行调整:

library(ggplot2)

data_2 |&gt;
  ggplot(aes(x = p_val, y = fct_reorder(name, p_val))) +
  geom_bar(aes(fill = z_score), stat = 'identity') +
  scale_fill_gradientn(
    colours = colorRampPalette(c('#709AE1', '#FFFFFF', '#FD7446'))(100),
    breaks=c(-4,0,4),
    limits=c(-4, 4)
  )
英文:

We could adapt with the breaks and limits arguments of scale_fill_gradientn function:

library(ggplot2)

data_2 |&gt;
  ggplot(aes(x = p_val, y = fct_reorder(name, p_val))) +
  geom_bar(aes(fill = z_score), stat = &#39;identity&#39;) +
  scale_fill_gradientn(
    colours = colorRampPalette(c(&#39;#709AE1&#39;, &#39;#FFFFFF&#39;, &#39;#FD7446&#39;))(100),
    breaks=c(-4,0,4),
    limits=c(-4, 4)
  )

如何在 ggplot 中设置具有固定中点的渐变色标?

huangapple
  • 本文由 发表于 2023年3月9日 22:18:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685834.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定