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

发布于 2024-11-26 23:23:51 字数 1447 浏览 2 评论 0原文

与 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

How can I improve the legend of spatial raster map plot using ggplot when compared to a spplot() legend?

I would like plot spatial maps using ggplot() instead of ssplot() however there are a few things that I would like to improve when compared to the spplot:

  1. create a ggplot legend that goes from small (bottom) to large values (top)
  2. Have the breaks in the ggplot legend similar to the ssplot() legend so that I know what the boundaries are of each color.

## 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() result
ssplot

ggplot() result
ggplot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泪之魂 2024-12-03 23:23:51

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

thanks @joran for the pointer to that.

here is an example code and output using the dev version:

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))

enter image description here

you can probably try this by:

# 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)

UPDATED:

here is the instruction for the installation from scratch:

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")
椵侞 2024-12-03 23:23:51

我不知道如何解决(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)

它在我的计算机上生成了此图表:

在此处输入图像描述

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

I'm not sure how to address (1) off the top of my head. But here are some possible solutions for (2) and (3).

I don't believe that ggplot2 currently is capable of labeling the legend in that manner. However, Koske has been working on some code that may be rolled into ggplot2 in the future that creates legends that are styled in this manner. Here is a link, although it will require installing some additional packages and is only alpha.

To get the specific breaks you're looking for, try this:

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)

which produced this graph on my machine:

enter image description here

I have not tested what this would look like using the experimental code mentioned above.

短暂陪伴 2024-12-03 23:23:51

回复:(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

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

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

Re: (2)
First cut() your data to get a binned dataset. Use breaks and labels options in cut() to obtain the correct labels, e.g.:

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

Then you can plot using ggplot and shifting the labels so that they are right on the color boudaries using:

 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

See also:
Generating a color legend with shifted labels using ggplot2

Alternatively, you can create the color legend with an external script (e.g. GrADS makes nice color legends using the right scripts) and specify the same colors manually in scale_fill_manual.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文