Is it possible to define the “mid” range in scale_fill_gradient2()?

会有一股神秘感。 提交于 2019-11-27 05:21:31

You can try scale_fill_gradientn and the values argument. From ?scale_fill_gradientn:

if colours should not be evenly positioned along the gradient this vector gives the position (between 0 and 1) for each colour in the colours vector. See rescale for a convience function to map an arbitrary range to between 0 and 1.

Thus, resolution of the colour scale for values close to zero may be increased by using suitable numbers in values = rescale(...).

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

Try replacing your scale_fill_gradient2 with:

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

You can adjust how narrow you want the black part to be by changing the .6 and .4 values (and also potentially the colors around "black"). This produces:

Compare to what you had:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!