我需要通过ggplot2调整热图的中点位置。我在Google周围搜索,发现scale_fill_gradient2
非常适合,但颜色似乎与我要寻找的不匹配。我知道z需要从0到1的范围。以下示例数据集生成:
library(ggplot2)
library(tibble)
library(RColorBrewer)
set.seed(5)
df <- as_tibble(expand.grid(x = -5:5,y = 0:5,z = NA))
df$z <- runif(length(df$z),min = 0,max = 1)
我尝试使用scale_fill_gradient2
进行绘制,但是蓝色并不像我所希望的那样呈“深色”。
ggplot(df,aes(x = x,y = y)) +
geom_tile(aes(fill = z)) +
scale_fill_gradient2(
low = 'red',mid = 'white',high = 'blue',midpoint = 0.7,guide = 'colourbar',aesthetics = 'fill'
) +
scale_x_continuous(expand = c(0,0),breaks = unique(df$x)) +
scale_y_continuous(expand = c(0,breaks = unique(df$y))
因此,我将scale_fill_distiller
与调色板“ RdBu”一起使用,该调色板带有我需要的色标,但范围和中点不正确。
ggplot(df,y = y)) +
geom_tile(aes(fill = z)) +
scale_fill_distiller(palette = 'RdBu') +
scale_x_continuous(expand = c(0,breaks = unique(df$x)) +
scale_y_continuous(expand = c(0,breaks = unique(df$y))
是否有办法获得第二个色标,但可以选择将中点范围设置为第一个?