如何在美国地图上为海蓝色?

Abe*_*Abe 11 maps plot r image

我想在图像上绘制美国地图,然后填写海洋.

这是我的出发点:

library(maps)
library(graphics)
image(x=-90:-75, y = 25:40, z = outer(1:15, 1:15, "+"), 
      xlab = "lon", ylab = "lat")
map("state", add = TRUE)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

但我希望大西洋和墨西哥湾能够以纯色填充.

And*_*our 19

好问题!这个怎么样? 屏幕抓取

library(maps)
image(x=-90:-75, y = 25:40, z = outer(1:15, 1:15, "+"), 
      xlab = "lon", ylab = "lat")
map("state", add = TRUE)

library(grid)
outline <- map("usa", plot=FALSE) # returns a list of x/y coords
xrange <- range(outline$x, na.rm=TRUE) # get bounding box
yrange <- range(outline$y, na.rm=TRUE)
xbox <- xrange + c(-2, 2)
ybox <- yrange + c(-2, 2)
# create the grid path in the current device
polypath(c(outline$x, NA, c(xbox, rev(xbox))),
         c(outline$y, NA, rep(ybox, each=2)),
         col="light blue", rule="evenodd")
Run Code Online (Sandbox Code Playgroud)

在阅读了grid关于网格路径的最新R-Journal文章(此处为pdf)之后,我阅读了Paul Murrell(背后的男人)后遇到了这个问题的解决方案.

记得:

"这不是你画的,而是你不画的东西"-Paul Murrell(R Journal Vol.4/2)


mds*_*ner 5

这是解决方案的一个变体,它通过相交/差分多边形来完成工作。数据集wrld_simpl可以由任何其他 SpatialPolygons* 对象替换。

library(maptools)
library(raster)
library(rgeos)

data(wrld_simpl)

x <- list(x=-90:-75, y = 25:40, z = outer(1:15, 1:15, "+"))

## use raster to quickly generate the polymask
## (but also use image2Grid to handle corner coordinates)
r <- raster(image2Grid(x))
p <- as(extent(r), "SpatialPolygons")

wmap <- gIntersection(wrld_simpl, p)
oceanmap <- gDifference(p, wmap)

image(r)
plot(oceanmap, add = TRUE, col = "light blue")
Run Code Online (Sandbox Code Playgroud)

由多边形交集/差分组成的海洋地图

(将地图数据转换为此可能很困难,我无法轻松完成maptools::map2SpatialPolygons,需要一些解决方法)