8

与 spplot() 图例相比,如何使用 ggplot 改进空间栅格地图图的图例?

我想使用 ggplot() 而不是 ssplot() 绘制空间地图,但是与 spplot 相比,我想改进一些事情:

  1. 创建一个从小(底部)到大值(顶部)的 ggplot 图例
  2. 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() 结果 ssplot

ggplot() 结果 ggplot

4

3 回答 3

5

感谢@joran 提供的指向。

这是使用开发版本的示例代码和输出:

br <- seq(min(df$NDVI), max(df$NDVI), len=8)

ggplot(data=df) + 
  geom_tile(aes(x, y, fill=NDVI)) + 
  scale_fill_gradient(low="red", high="green", 
    breaks=br, labels=sprintf("%.02f", br), 
    guide=guide_colorbar(title=NULL, nbin=100, barheight=unit(0.75, "npc"), label.hjust=1)) + 
  scale_x_continuous(expand=c(0,0)) + 
  scale_y_continuous(expand=c(0,0))

在此处输入图像描述

您可以通过以下方式尝试:

# from Hadley's instruction
install.packages("devtools")
library(devtools)
dev_mode() # to avoid interfering with your existing install
install_github("ggplot2", username="kohske", branch = "feature/new-guides-with-gtable")
library(ggplot2)

更新:

这是从头开始安装的说明:

install.packages(
  c('devtools', 'digest', 'memoise', 'plyr', 'reshape2', 'RColorBrewer', 'stringr', 'dichromat', 'munsell', 'plyr', 'colorspace'), 
  dep=TRUE)

library(devtools)
dev_mode()

install_github("scales")
install_github("ggplot2", username="kohske", branch = "feature/new-guides-with-gtable")
于 2011-07-29T16:56:08.600 回答
4

我不确定如何解决(1)的问题。但这里有一些可能的解决方案(2)和(3)。

我不相信ggplot2目前能够以这种方式标记传奇。然而,Koske 一直在编写一些代码,这些代码可能会ggplot2在未来推出,以创建以这种方式设计的图例。是一个链接,尽管它需要安装一些额外的软件包并且只是 alpha。

要获得您正在寻找的特定休息时间,请尝试以下操作:

br <- c(-0.25,-0.05,0.15,0.35,0.56,0.76,0.96,1.16)
p  <- ggplot(data=df) + geom_tile(aes(x, y, fill=NDVI)) +
coord_equal() + labs(x=NULL, y=NULL) + 
scale_fill_gradient(low="red", mid="yellow",high="green",
breaks = br) + theme_bw() +
scale_x_continuous(expand=c(0,0)) + scale_y_continuous(expand=c(0,0))
print(p)

它在我的机器上生成了这张图:

在此处输入图像描述

我还没有使用上面提到的实验代码测试过这会是什么样子。

于 2011-07-28T20:06:08.613 回答
0

回复:(2)首先cut()你的数据得到一个分箱数据集。使用breakslabels选项cut()来获得正确的标签,例如:

dat$col <- cut(
  df$NDVI, 
  breaks=c(-Inf, -0.25, 0.05, ...whatever..., Inf), 
  labels=c(-0.25, 0.05, ...whatever..., "")
)

然后,您可以使用 ggplot 进行绘图并移动标签,使它们位于颜色边界上,使用:

 scale_fill_manual (your options...) + guides(fill=guide_legend(label.vjust = 1.2)) #1.2= what fits your legend best. Use label.hjust if using an horizontal color bar

另请参阅: 使用 ggplot2 生成带有移位标签的颜色图例

或者,您可以使用外部脚本创建颜色图例(例如,GrADS 使用正确的脚本制作漂亮的颜色图例)并在 scale_fill_manual 中手动指定相同的颜色。

于 2014-11-20T11:32:54.420 回答