是否可以在scale_fill_gradient2()中定义“中间”范围?

50pmv0ei  于 2023-03-27  发布在  其他
关注(0)|答案(3)|浏览(192)

我正在使用ggplot()创建一个热图,并希望使用scale_fill_gradient2()的3种配色方案。然而,我发现中间颜色太宽,倾向于负面显示我的一些数据(例如使用“黑色”)。是否可以定义被认为是“中间”的范围,使其更窄?如果没有,是否有更好的方法可以这样做?
数据集:

structure(list(var1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L), .Label = c("A", "B", "C", "D", "E", "F", "G"), class = "factor"), 
var2 = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 
23L, 24L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 
17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 
19L, 20L, 21L, 22L, 23L, 24L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 
20L, 21L, 22L, 23L, 24L), .Label = c("a", "b", "c", "d", 
"e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", 
"q", "r", "s", "t", "u", "v", "w", "x"), class = "factor"), 
corr = c(0.039063517, -0.012531832, 0.096287532, -0.156156609, 
-0.097044878, 0.144426494, 0.102142979, 0.061426893, 0.051079225, 
0.271860908, 0.156812951, 0.259456277, -0.121838722, -0.157440078, 
-0.037827967, -0.01929319, -0.108895665, -0.066815122, 0.254285337, 
0.12688199, -0.064394035, 0.00112601, 0.173774179, 0.01179886, 
0.105171013, 0.088559148, 0.033584364, -0.368075609, -0.272671354, 
-0.456557935, -0.441008229, -0.118498286, -0.309056047, 0.051624421, 
0.087594347, -0.0264506, 0.081249807, -0.194887615, -0.135397719, 
-0.078688964, -0.059544125, -0.065410158, 0.211446055, 0.027338504, 
-0.06185598, -0.007720807, 0.092997248, -0.177812491, 0.133226267, 
0.075247459, 0.04586679, -0.37972917, -0.254410003, -0.447919321, 
-0.426264017, -0.150347417, -0.270786314, 0.143483685, 0.230384468, 
-0.012297462, 0.096957204, -0.134348613, -0.056239035, -0.038059581, 
-0.040273741, -0.131126698, 0.222754865, 0.067883188, -0.154724805, 
-0.076366467, 0.152747678, -0.160657826, 0.104652439, 0.029599007, 
-0.02194356, -0.349623751, -0.228046427, -0.363148102, -0.326649794, 
-0.160199177, -0.253621489, 0.242711785, 0.242808683, 0.005937022, 
0.120849407, -0.131358491, 0.004558011, -0.056873299, 0.048745675, 
-0.147360978, 0.204314713, 0.033110005, -0.154644375, -0.095999454, 
0.202067265, -0.182694112, 0.071256125, 0.062783632, 0.078250678, 
-0.348208521, -0.237899511, -0.235431913, -0.249280276, -0.129802124, 
-0.084153729, 0.201859004, 0.292353092, -0.000270168, 0.130668616, 
-0.101519227, -0.024695776, -0.024373331, -0.05976031, -0.07861482, 
0.150316096, 0.100494694, -0.107507982, -0.06543542, 0.220813403, 
-0.159374132, 0.139677124, 0.005974473, 0.011484774, -0.423918147, 
-0.286712875, -0.286979217, -0.2730134, -0.095014441, -0.134216671, 
0.215551068, 0.432571228, 0.029945066, 0.178996421, -0.114005412, 
-0.098205883, 0.009661061, -0.07163473, 0.010722592, 0.175801779, 
0.170582102, -0.199407024, -0.15933157, 0.246575881, -0.084929336, 
0.160642274, 0.025941888, -0.058676465, -0.297996589, -0.180504241, 
-0.236672516, -0.228203539, -0.181349045, -0.091893083, 0.277145017, 
0.38119709, 0.019554381, 0.18725277, -0.019416086, 0.000417258, 
0.134560252, 0.059330986, -0.091994882, 0.11807573, 0.175219716, 
-0.169272253, -0.094240107, 0.180532375, 0.04322002)), .Names = c("var1", "var2", "corr"), class = "data.frame", row.names = c(NA, -168L))

使用reshape2ggplot2scales的代码:

sample.m <- melt(sample)
sample.m$var2 <- as.character(sample.m$var2)
sample.m$var2 <- factor(sample.m$var2, levels=unique(sample.m$var2),ordered=TRUE)
sample.m$var1 <- as.character(sample.m$var1)
sample.m$var1 <- factor(sample.m$var1, levels=unique(sample.m$var1),ordered=TRUE)
p <- ggplot(sample.m, aes(var2,var1)) + geom_tile(aes(fill=value), colour="transparent") +    scale_fill_gradient2(low="cyan", mid="black", high="red", midpoint=0, limits=c(-0.6,0.6))
base_size <- 9
p + theme_bw() + labs(x = "", y = "") + scale_x_discrete(expand = c(0,0)) + theme(legend.position = "bottom", axis.ticks = element_blank(), axis.text.x = element_text(size = base_size * 0.8, angle = 310, hjust = 0, colour = "black"), axis.text.y = element_text(size = base_size * 0.8,colour = "black"))
rlcwz9us

rlcwz9us1#

您可以尝试scale_fill_gradientnvalues参数。从?scale_fill_gradientn开始:
如果颜色不应该沿着梯度均匀地定位,则该向量给出颜色向量中每个颜色的位置(在0和1之间)。参见rescale以获得将任意范围Map到0和1之间的方便函数。
因此,通过使用values = rescale(...)中的适当数字,可以增加接近零的值的色标的分辨率。

scale_fill_gradientn(colours = c("cyan", "black", "red"),
                       values = scales::rescale(c(-0.5, -0.05, 0, 0.05, 0.5)))

v8wbuo2f

v8wbuo2f2#

尝试将scale_fill_gradient2替换为:

scale_fill_gradientn(values=c(1, .6, .5, .4, 0), colours=c("red", "#770000", "black", "#007777", "cyan"))

您可以通过更改.6.4值(以及“黑色”周围的颜色)来调整黑色部分的窄度。这会产生:

对比一下你所拥有的:

a9wyjsp7

a9wyjsp73#

从2023年3月21日起,您可以简单地将midpoint设置为scale_fill_gradient2中的某个值

scale_fill_gradient2(midpoint = 1000)

也可以选择中点颜色

scale_fill_gradient2(midpoint = 1000, low = "green", mid = "blue", high = "red")

相关问题