2013-05-02 110 views
11

我想绘制一幅美国地图,然后填写海洋图。如何在美国的地图上为海洋蓝色着色?

这里是我的出发点:

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) 

enter image description here

但我想墨西哥的大西洋和墨西哥湾的纯色填充。

+1

它可能会更容易从蓝色背景开始,然后掩盖图像/热图上的状态边界。 – 2013-05-02 00:48:21

+1

或者有一个单独的海洋多边形(我认为'rgeos'有一个差异函数,如果你还没有这样一个多边形)。 – 2013-05-02 01:35:14

回答

19

好问题!这个怎么样? screen grab

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

阅读保罗的Murrell的(背后的男人grid)网格路径(pdf here)最近的R-杂志的文章后,我整个解决这个问题就来了。

记住:

“这不是你画什么,这是什么,你不画” - 保罗马雷尔

+0

+1不错的解决方案。 – 2013-05-02 09:07:23

+0

+1为您的来源提供全部功劳。 – 2013-05-02 11:48:18

4

这里的一个变种(R杂志卷4/2)。解决方案通过相交/差分多边形完成工作。数据集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") 

oceanmap by poly intersection/differencing

(地图数据转换到这可能是艰难的,我是不会轻易与maptools::map2SpatialPolygons做到这一点,它会采取一些变通方法)

3

我可以回答你的问题的标题( “我怎样才能在美国的地图上染上蓝色的海洋?”),尽管不是在你的问题的主体中描述的具体情况(“我想画一幅美国的地图 ,然后填写海洋“)。

但是,如果对其他遇到您的问题的人有帮助,我将包含此答案。

map(database='state', bg='light blue') 

bg选项提供的淡蓝色到地图的背景,其中包括海洋的颜色。

相关问题