How to Color the Ocean Blue in a Map of the Us

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

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)

Adding color to the ocean in R

Bit of a hack, but you can plot a rectangle the same size as the plotting area, then use par(new = TRUE) to plot mapPlot on top:

par(mar=c(2, 2, 1, 1))
plot(0, 0, type="n", ann=FALSE, axes=FALSE)
user <- par("usr")
rect(user[1], user[3], user[2], user[4],
col="lightblue", border=NA)
par(new = TRUE)
lonlim <- c(-79, -76)
latlim <- c(33, 35)
mapPlot(coastlineWorldFine,
projection="+proj=moll",
col = "lightgray",
longitudelim=lonlim,
latitudelim=latlim,
grid = FALSE)

Sample Image

mask oceans - wrld_simpl

My interpretation of your question is that you want to show the raster over the land surface but not over the ocean. To do this it is easier to mask the raster by the land surface, I disaggregated the raster first to make the edges smoother:

#sample raster
r <- raster(ncol=10, nrow=20)
r[] <- 1:ncell(r)
extent(r) <- extent(c(-20, 20, -20, 20))

r=disaggregate(r,10)
r=mask(r,wrld_simpl)

#plotting
var_df <- as.data.frame(rasterToPoints(r))
p <- ggplot() +
geom_polygon(data = wrld_simpl[wrld_simpl@data$UN!="10",],
aes(x = long, y = lat, group = group),
colour = "black", fill = "grey") # does the fortification automatically
p <- p + geom_raster(data = var_df, aes(x = x, y = y, fill = layer))
p <- p + coord_equal() + theme_bw() +labs(x="", y="")
p <- p + theme(legend.key=element_blank(),
axis.text.y =element_text(size=16),
axis.text.x =element_text(size=16),
legend.text =element_text(size=12),
legend.title=element_text(size=12))
p <- p + scale_fill_gradientn(colours = rev(terrain.colors(10)))
p <- p + geom_polygon(data = wrld_simpl[wrld_simpl@data$UN!="10",],
aes(x = long, y = lat, group = group),
colour = "black", fill = NA)
p

Sample Image

How can I color ocean with topojson in d3 when I have coordinate info for land?

There's no need (and it would be pretty difficult and somewhat expensive computationally) to figure out the inverse of the landmass. But you can just color the background.

I.e you can use CSS:

svg {
background: lightBlue;
}

or you can prepend a <rect> element with a blue fill behind the map:

svg.append('rect')
.attr('width', mapWidth)
.attr('height', mapHeight)
.attr('fill', 'lightBlue')


Related Topics



Leave a reply



Submit