[英]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.