【发布时间】:2014-11-20 19:42:06
【问题描述】:
与 spplot() 图例相比,如何使用 ggplot 改进空间栅格地图图的图例?
我想使用 ggplot() 而不是 ssplot() 绘制空间地图,但是与 spplot 相比,我想改进一些事情:
- 创建一个从小(底部)到大值(顶部)的 ggplot 图例
- 在 ggplot 图例中设置与 ssplot() 图例类似的断点,以便我知道每种颜色的边界是什么。
## load packages
require(raster)
require(ggplot2)
require(rgdal)
require(RColorBrewer)
set.seed(1)
r <- raster(xmn=-110, xmx=-90, ymn=40, ymx=60, ncols=40, nrows=40,
crs="+proj=lcc +lat_1=48 +lat_2=33 +lon_0=-100
+ellps=WGS84")
r <- setValues(r,matrix(rnorm(1600, mean=0.4,sd=0.2)))
## 1. spatial map with spplot
cuts <-seq(minValue(r),maxValue(r),length.out=8)
cuts = round(cuts,digits=2)
col.regions = brewer.pal(length(cuts)+3-1, "RdYlGn")
print(
spplot(as(r, 'SpatialGridDataFrame'),at=cuts,
col.regions=col.regions,
colorkey=list(labels=list(at=cuts),at=cuts), pretty=TRUE,
scales=list(draw=T)
)
)
## 2. spatial map with ggplot
p = rasterToPoints(r); df = data.frame(p)
colnames(df) = c("x", "y", "NDVI")
p <- ggplot(data=df) + geom_tile(aes(x, y, fill=NDVI)) +
coord_equal() + labs(x=NULL, y=NULL) +
scale_fill_gradient2(low="red", mid="yellow",high="green",
limits=c(minValue(r),maxValue(r)), midpoint = 0.4) + theme_bw() +
scale_x_continuous(expand=c(0,0)) + scale_y_continuous(expand=c(0,0))
print(p)
ssplot() 结果
ggplot() 结果
【问题讨论】:
-
请不要使用红绿色对比。 5-10% 的红绿色弱者会找到他们very hard to read。
-
Koske 网站的链接 - 或多或少地说明了我想做的事情。我将进一步探索 ggplot() 容量、调整颜色、改进图例并在下面发布更新。欢迎所有提示/示例。谢谢
标签: r map ggplot2 spatial raster