我试图使用ggplot2而不是光栅包绘图功能制作光栅图,原因有些不相关.
我想缩放颜色,使图上的最低温度为蓝色,图上的最高温度为红色,而中间范围为白色.我已经尝试了ggplot2的众多功能,但我一直未能获得理想的结果.这就是我要的:

这是我的ggplot2代码的当前状态:
library(raster)
library(ggplot2)
library(scales)
r = raster()
r[] = 1:ncell(r)
df = as.data.frame(r, xy=TRUE)
the_plot = ggplot(df) +
geom_raster(aes(x, y, fill=layer)) +
scale_fill_gradient2(low=muted('red'), mid='white', high=muted('blue'))
print(the_plot)
Run Code Online (Sandbox Code Playgroud)
哪个,而不是所需的颜色渐变,产生这个:

任何建议都非常感谢!