相关疑难解决方法(0)

在ggplot2中更改栅格图的颜色

我试图使用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)

哪个,而不是所需的颜色渐变,产生这个:

ggplot2 scale_fill_gradient2()失败

任何建议都非常感谢!

gis plot r ggplot2

9
推荐指数
1
解决办法
1万
查看次数

标签 统计

ggplot2 ×1

gis ×1

plot ×1

r ×1