【问题标题】:How to improve a spatial raster map using ggplot when compared to spplot?与 spplot 相比,如何使用 ggplot 改进空间栅格图?
【发布时间】:2014-11-20 19:42:06
【问题描述】:

与 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() 结果

ggplot() 结果

【问题讨论】:

  • 请不要使用红绿色对比。 5-10% 的红绿色弱者会找到他们very hard to read
  • Koske 网站的链接 - 或多或少地说明了我想做的事情。我将进一步探索 ggplot() 容量、调整颜色、改进图例并在下面发布更新。欢迎所有提示/示例。谢谢

标签: r map ggplot2 spatial raster


【解决方案1】:

感谢@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")

【讨论】:

  • 我刚刚尝试过,但出现以下错误:构建 'ggplot2_0.9.0.tar.gz' 错误:依赖项 'scales' 不适用于包 'ggplot2',它说 scales 是不适用于 2.13.0 我在哪里可以找到它?制作像上面这样的传说会很棒。太好了。
  • 我在 github github.com/hadley/scales 上找到了 scales,在 R 中安装它的最佳方法是什么?
  • 答案已更新。试试install_github("scales")。如果遇到错误,例如缺少包RColorBrewer, stringr, dichromat, munsell, plyr, colorspace,请尝试install.packages(c('RColorBrewer', 'stringr', 'dichromat', 'munsell', 'plyr', 'colorspace'), dep=TRUE)
  • 非常感谢!这很好用。我现在将尝试改进颜色,使不同的级别都有清晰的颜色(关于 Hadley 的话。
【解决方案2】:

我不知道如何解决我头顶上的 (1)。但是对于 (2) 和 (3),这里有一些可能的解决方案。

我不相信ggplot2 目前能够以这种方式标记图例。然而,Koske 一直在研究一些代码,这些代码将来可能会被纳入ggplot2,以创建以这种方式设计的图例。 Here 是一个链接,虽然它需要安装一些额外的包并且只是 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)

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

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

【讨论】:

  • 我能想到的在不破坏 ggplot 代码内部的情况下获得从高到低的图例的唯一方法是使用 cut 将比例变成一个因子,手动分配颜色, 并颠倒关卡的顺序 ...
  • 感谢 Koske 网站的链接,这正是我想要对 ggplot 空间图的图例所做的。我会做一些测试,很快就会在下面发布最终结果。
  • 请注意,代码已过时。请在 ggplot2 列表中找到此帖子:groups.google.com/group/ggplot2/browse_thread/thread/…
【解决方案3】:

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

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

另请参阅: Generating a color legend with shifted labels using ggplot2

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

【讨论】:

    猜你喜欢
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-03
    相关资源
    最近更新 更多