简体   繁体   English

在热图中为比例设置 scale_fill_gradientn 的限制(从 0 到 1 的值带有蓝色刻度,0 是白色,大于 1 的值带有红色刻度)

[英]Set limits for scale_fill_gradientn for ratios (values from 0 to 1 with blue scale, 0 is white and values over 1 with red scale) in heatmap

I'm trying to plot different colors for my ratios in a general way (for example case/control)我正在尝试以一般方式(例如案例/控制)为我的比率 plot 不同的 colors

For example, if we have values between 1 and 0 (which means the value of control is higher), I have a scale from white to blue.例如,如果我们有 1 和 0 之间的值(这意味着控制的值更高),我有一个从白色到蓝色的刻度。 But if the value is between 1 and +inf, then we have that the case is higher in value and the scale is from white to red.但是如果该值在 1 和 +inf 之间,那么我们就认为该情况的值较高,并且刻度是从白色到红色。

I'm plotting like this:我正在绘制这样的图:

mtcars2 <- mtcars
mtcars2$ratio1 <- mtcars$mpg / mtcars$disp
mtcars2$ratio2 <- mtcars$disp / mtcars$mpg
mtcars2$ratio3 <- mtcars$drat / mtcars$wt

a <- max(mtcars2$ratio1)

b <- ifelse(a < 2, 2, a) #so I get a value of b which is above 2 to create the scale in values


ggplot(mtcars, aes(x = rownames(mtcars), y = 1, fill = mtcars2$ratio1)) +
  geom_tile() +
  theme_minimal() +
  scale_fill_gradientn(
    colours = c("blue", "white", "red"),
    na.value = "grey98",
    name = "ratio",
    values = c(0, 1, b)
  ) +
  theme_classic() +
  theme(axis.text.x = element_text(
    size = 10, face = "bold", color = "black",
    angle = 45, hjust = 1
  )) #b should be always above 1 (I selected 2)


a <- max(mtcars2$ratio2)

b <- ifelse(a < 2, 2, a)


ggplot(mtcars, aes(x = rownames(mtcars), y = 1, fill = mtcars2$ratio2)) +
  geom_tile() +
  theme_minimal() +
  scale_fill_gradientn(
    colours = c("blue", "white", "red"),
    na.value = "grey98",
    name = "ratio",
    values = c(0, 1, b)
  ) +
  theme_classic() +
  theme(axis.text.x = element_text(
    size = 10, face = "bold", color = "black",
    angle = 45, hjust = 1
  ))


a <- max(mtcars2$ratio3)

b <- ifelse(a < 2, 2, a)


ggplot(mtcars, aes(x = rownames(mtcars), y = 1, fill = mtcars2$ratio3)) +
  geom_tile() +
  theme_minimal() +
  scale_fill_gradientn(
    colours = c("blue", "white", "red"),
    na.value = "grey98",
    name = "ratio",
    values = c(0, 1, b)
  ) +
  theme_classic() +
  theme(axis.text.x = element_text(
    size = 10, face = "bold", color = "black",
    angle = 45, hjust = 1
  ))

Which works fine for values between 0 and 1 (when nothing over 1 is present), but does not plot red color for values between 1 and max ratio value and when mixed values are present (between 0 and 1, and 1 and +inf)对于 0 和 1 之间的值(当不存在超过 1 时),它可以正常工作,但对于介于 1 和最大比率值之间的值以及存在混合值(在 0 和 1 之间,以及 1 和 +inf 之间)时,plot 不

I can't consider values of log nor normalized values!我不能考虑日志值或标准化值! I need the scale like it is我需要像现在这样的规模

Using使用

a = ifelse(max(mtcars2$ratio3) > 1, max(mtcars2$ratio3), 1)

b = ifelse(max(mtcars2$ratio3) > 1, 1/max(mtcars2$ratio3), 1)

c = ifelse(b == 1, 0.99, b)
           
          

ggplot(mtcars2, aes(x = rownames(mtcars), y = 1, fill = ratio3)) +
  geom_tile() +
  theme_minimal() +
  scale_fill_gradientn(
    colours = c("blue", "white", "red"),
    na.value = "grey98",
    name = "ratio",
    limits = c(0, a),
    values = c(0, c , 1)
  ) +
  theme_classic() +
  theme(axis.text.x = element_text(
    size = 10, face = "bold", color = "black",
    angle = 45, hjust = 1
  ))

Works like a charm (adapted from Allan Cameron's answer)像魅力一样工作(改编自艾伦卡梅隆的回答)

The values argument has to be numbers between 0 and 1 (representing the upper and lower limits of the scale), so you need something like: values参数必须是介于 0 和 1 之间的数字(表示比例的上限和下限),因此您需要以下内容:

ggplot(mtcars2, aes(x = rownames(mtcars), y = 1, fill = ratio3)) +
  geom_tile() +
  theme_minimal() +
  scale_fill_gradientn(
    colours = c("blue", "white", "red"),
    na.value = "grey98",
    name = "ratio",
    limits = c(0, ifelse(max(mtcars2$ratio3) > 1, max(mtcars2$ratio3), 1)),
    values = c(0, ifelse(max(mtcars2$ratio3) > 1, 1/max(mtcars2$ratio3), 1), 1)
  ) +
  theme_classic() +
  theme(axis.text.x = element_text(
    size = 10, face = "bold", color = "black",
    angle = 45, hjust = 1
  ))

在此处输入图像描述

I think there are two options.我认为有两种选择。

Option 1: If you want the scale to be symmetric around 1, you can set the rescaler argument of the scale to set 1 as the middle.选项 1:如果您希望比例尺围绕 1 对称,您可以将比例尺的rescaler参数设置为将 1 设置为中间。 This might work somewhat better after log-transform and setting 0 as the center.在对数转换并将 0 设置为中心之后,这可能会更好一些。

library(ggplot2)

set.seed(0)
df <- data.frame(
  x = 1:20,
  y = 2,
  value = rlnorm(20)
)

ggplot(df, aes(x, y, fill = value)) +
  geom_tile() +
  scale_fill_gradientn(
    colours=c("blue", "white", "red"), 
    na.value = "grey98", name = "ratio",
    rescaler = ~ scales::rescale_mid(.x, mid = 1)
  )

Option 2: If you don't particularly care if the scale is symmetric, you can use the values argument.选项 2:如果您不特别关心比例是否对称,则可以使用values参数。 I think the thing you might've overlooked in your code is that the values() should be between 0 and 1. To get the colours into that range, you need to divide by the maximum value.我认为您在代码中可能忽略的一点是values()应该介于 0 和 1 之间。要使颜色进入该范围,您需要除以最大值。


ggplot(df, aes(x, y, fill = value)) +
  geom_tile() +
  scale_fill_gradientn(
    colours=c("blue", "white", "red"), 
    na.value = "grey98", name = "ratio",
    values = c(0, 1, max(df$value))/max(df$value)
  )

Created on 2022-08-17 by the reprex package (v2.0.1)代表 package (v2.0.1) 于 2022 年 8 月 17 日创建

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 scale_fill_gradientn 中使用绝对值而不是相对值 - Use absolute values not relative values in scale_fill_gradientn 为负值和正值定义颜色渐变 scale_fill_gradientn() - define color gradient for negative and positive values scale_fill_gradientn() 用ggplot2的scale_fill_gradientn表示,恒定渐变 - Representation with, scale_fill_gradientn ggplot2, constant gradient 如何获得多个具有相同比例的 ggplot2 scale_fill_gradientn? - How to get multiple ggplot2 scale_fill_gradientn with same scale? 使用 scale_fill_gradientn() 将颜色比例转换为概率转换颜色分布 - Transform color scale to probability-transformed color distribution with scale_fill_gradientn() R:使用带有geom_tile()和scale_fill_gradientn()的颜色划分热图 - R: Dividing heat map using colors with geom_tile() and scale_fill_gradientn() 使用 scale_fill_gradientn 将特定颜色分配给条形 plot 中的确定值 - Assign specific color to definite value in bar plot using scale_fill_gradientn scale_fill_gradientn() 成功填充 map 但未能在图例中显示所有 colors - The scale_fill_gradientn() successfully fills map but fails showing all colors in the legend 在ggplot中与geom_bin2d结合获得相对计数,并正确使用scale_fill_gradientn - Get relative count in ggplot combined with geom_bin2d, and proper use of scale_fill_gradientn 使用转换后的颜色/填充变量 ggplot2 为 scale_*_gradientn 指定手动值 - Specify manual values for scale_*_gradientn with transformed color/fill variable ggplot2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM