Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

r - How can I color the ocean blue in a map of the US?

I would like to draw a map of the US over an image, but then fill in the oceans.

here is my starting point:

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

But I would like the Atlantic Ocean and Gulf of Mexico to be filled in a solid color.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Good question! How's this? 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")

I came across the solution to this problem after reading Paul Murrell's (the man behind grid) recent R-Journal article on grid paths (pdf here).

Remember:

"It’s Not What You Draw, It’s What You Don’t Draw" -Paul Murrell (R Journal Vol. 4/2)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...