英文:
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)
)
以上代码按预期工作。现在,假设有一个只有负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
值都为负):
我尝试更改填充颜色,以便在所有z_score
值都为负的情况下只有白色和蓝色,但这会产生不正确的图:
对于仅有负值的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 <- 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)
)
The code above works as expected. Now, suppose a data with only negative 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)
)
This produces incorrect plot, since I want colors to map from white to blue (all z_score
values are negative):
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:
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
函数是否达到你的目标。你可以为 low
、high
和 mid
值设置颜色。默认情况下,中点设置为零,并使用白色。
使用此函数,无论值是负数还是正数,都可以使用相同的填充比例。
library(tidyverse)
data_1 |>
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')
data_2 |>
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')
英文:
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 |>
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')
data_2 |>
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')
答案2
得分: 0
我们可以使用scale_fill_gradientn
函数的breaks
和limits
参数进行调整:
library(ggplot2)
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),
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 |>
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)
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论