与 spplot 相比,如何使用 ggplot 改进空间栅格地图?
与 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() 结果
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:
- create a ggplot legend that goes from small (bottom) to large values (top)
- 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
ggplot() result
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
感谢@joran 的指点。
这是使用开发版本的示例代码和输出:
您可以通过以下方式尝试:
更新:
此处是从头开始安装的说明:
thanks @joran for the pointer to that.
here is an example code and output using the dev version:
you can probably try this by:
UPDATED:
here is the instruction for the installation from scratch:
我不知道如何解决(1)的问题。但这里有一些可能的解决方案(2)和(3)。
我不相信 ggplot2 目前能够以这种方式标记图例。然而,Koske 一直在开发一些代码,这些代码将来可能会被集成到 ggplot2 中,从而创建以这种方式设计的图例。 这里是链接,尽管它需要安装一些额外的软件包并且只是 alpha。
要获得您正在寻找的特定中断,请尝试以下操作:
它在我的计算机上生成了此图表:
我还没有使用上面提到的实验代码测试这会是什么样子。
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 intoggplot2
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:
which produced this graph on my machine:
I have not tested what this would look like using the experimental code mentioned above.
回复:(2)
首先
cut()
您的数据以获得分箱数据集。在cut()
中使用breaks
和labels
选项来获取正确的标签,例如:然后您可以使用 ggplot 进行绘图并移动标签,以便他们在颜色边界上使用:
另请参阅:
使用 ggplot2 生成带有移位标签的颜色图例
,您可以使用外部脚本创建颜色图例(例如,GrADS 使用正确的脚本创建漂亮的颜色图例),并在scale_fill_manual 中手动指定相同的颜色。
Re: (2)
First
cut()
your data to get a binned dataset. Usebreaks
andlabels
options incut()
to obtain the correct labels, e.g.:Then you can plot using ggplot and shifting the labels so that they are right on the color boudaries using:
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.