1

我想在澳大利亚部分地区的地图中掩盖海洋。

这是我的出发点:

library(maps)
library(mapdata)
image(x=110:155, y =-40:-10, z = outer(1:45, 1:30, "+"), 
  xlab = "lon", ylab = "lat")

然后,按照此处发布的解决方案(如何在美国地图中将海洋染成蓝色?),我设置了多路径:

outline <- map("worldHires", 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")][1]

然而,由此产生的情节在国家边界的两侧都被掩盖了。谁能帮我只在国外戴口罩?

4

1 回答 1

1

感谢此解决方案的 R-help 邮件列表(https://stat.ethz.ch/pipermail/r-help/2013-July/356943.html):

library(maps)
library(mapdata)

image(x=110:155, y =-40:-10, z = outer(1:45, 1:30, "+"),
  xlab = "lon", ylab = "lat")

明确说明地区名称(“澳大利亚”):

outline <- map("worldHires", regions="Australia", exact=TRUE, 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)

从大纲中省略 NA(这是之前干扰路径的内容):

subset <- !is.na(outline$x)

使用轮廓的子集创建多路径(即没有 NA):

# create the grid path in the current device
polypath(c(outline$x[subset], NA, c(xbox, rev(xbox))),
  c(outline$y[subset], NA, rep(ybox, each=2)),
  col="light blue", rule="evenodd")
于 2013-07-17T13:04:36.967 回答