【发布时间】:2018-02-01 22:23:13
【问题描述】:
我正在尝试在 R 的地图上绘制取自栅格数据集的两个变量,以生成看起来有点像这样的东西:
但是,理想情况下,我希望从左下角到右上角的比例为灰度(从浅灰色到黑色),从而突出显示两个变量差异很小的区域。
到目前为止,这就是我使用包colorplaner:
#load packages
require(raster)
require(colorplaner)
require(ggplot2)
#here's some dummy data
r1<- raster(ncol=10, nrow=10)
set.seed(0)
values(r1) <- runif(ncell(r1))
r2<- raster(ncol=10, nrow=10)
values(r2) <- runif(ncell(r2))
#here I create a grid with which I can extract information on the raster datasets
grid<-raster(ncol=10, nrow=10)
grid[] <- 1:ncell(grid)
grid.pdf<-as(grid, "SpatialPixelsDataFrame")
grid.pdf$r1<-(extract(r1,grid.pdf))
grid.pdf$r2<-(extract(r2,grid.pdf))
#here I convert the grid to a dataframe for plotting in ggplot2
grid.df<-as.data.frame(grid.pdf)
ggplot(data=grid.df,aes(x,y,fill=r1,fill2=r2))+geom_raster()+scale_fill_colourplane("")
这给了我这个:
这个默认色阶并不真正适合我的需要 - 我更喜欢看起来像这样的色阶,取自 this website:
但是我发现修改函数 scale_fill_colourplane 中的颜色方案很棘手
我能得到的最接近我想要的色阶是这样的:
ggplot(data=grid.df,aes(x,y,fill=r1,fill2=r2))+
geom_raster()+
scale_fill_colourplane(name = "",na.color = "white",
color_projection = "interpolate",vertical_color = "#FAE30C",
horizontal_color = "#0E91BE", zero_color = "#E8E6F2",
limits_y = c(0,1),limits=c(0,1))
有关于如何修改scale_fill_colourplane 函数here 中使用的色阶的信息,这使我看起来应该能够做我想做的事,但我不太明白。
有没有人知道如何实现我想要的?我愿意使用其他软件包,但如果可能的话,我更喜欢使用ggplot2 进行绘图,以便该数字与我目前正在制作的其他软件包一致。
【问题讨论】:
标签: r ggplot2 geospatial raster r-raster