Question

I would like to color grid cells in the United States and Canada. My goal is very similar to this question: R Plot Filled Longitude-Latitude Grid Cells on Map However, that question only deals with the United States and I cannot figure out how to add Canada.

I was able to draw a map of the U.S. and Canada by modifying code found here: https://groups.google.com/forum/#!topic/ggplot2/KAKhoE0GO4U

library(ggplot2)
library(rgeos)
library(maps)
library(maptools)

PolygonCoords <- function(polygon) {
  polygons <- polygon@Polygons
  coords.list <- lapply(seq_along(polygons), function(i) {
    # Extract the group, sequence, area, longitude, and latitude.
    coords <- polygons[[i]]@coords
    cbind(i, 1:nrow(coords), polygons[[i]]@area, coords)
  })
  coords.df <- as.data.frame(do.call(rbind, coords.list))
  names(coords.df) <- c("order", "seq", "area", "long", "lat")
  return(coords.df)
}

ConvertWorldSimple <- function(mapdata, min.area = 0) {

  coords.list <- lapply(mapdata@polygons, PolygonCoords)
  ncoords <- sapply(coords.list, nrow)
  coords.df <- do.call(rbind, coords.list)
  coords.df$country <- rep(mapdata@data$NAME, ncoords)
  country.group <- factor(paste(coords.df$country, coords.df$order))
  coords.df$group <- as.numeric(country.group)
  coords.df <- coords.df[coords.df$area >= min.area, ]
  return(coords.df)
}

data("wrld_simpl")
world <- ConvertWorldSimple(wrld_simpl, min.area = 0.1)

world <- world[world$country %in% c('United States', 'Canada'),]

na <- data.frame(
  country = c("United States", "Canada"),
  is.north.america = TRUE)

world <- merge(world, na, all.x = TRUE)
world$is.north.america[is.na(world$is.north.america)] <- FALSE

world <- world[order(world$order, world$seq), ]

ggplot(world, aes(long, lat, group = group)) +
  geom_polygon(aes(fill = is.north.america)) +
  geom_path(color = "white", size = 0.1) +
  scale_fill_manual(values = c("darkgray"), guide = "none") +
  scale_y_continuous("", breaks=(-2:2) * 30) +
  scale_x_continuous("", breaks=(-4:4) * 45) +
  coord_equal() +
  theme_bw()

Here is code to create fake attribute data for the grid cells, found here: http://www.numbertheory.nl/2011/11/08/drawing-polar-centered-spatial-maps-using-ggplot2/

set.seed(1234)

xlim = c(-110,-100)
ylim = c(40,60)

dat_grid = expand.grid(x = xlim[1]:xlim[2], y = ylim[1]:ylim[2])
dat_grid$z = runif(nrow(dat_grid))

head(dat_grid)

Here is the ggplot2 code used in the earlier Stack Overflow post to overlay a grid of attributes on a map of the Lower 48:

library(ggplot2)
library(maps)
us_states <- map_data("state")
(ggplot(aes(x=x,y=y,fill=z),data=dat_grid) + geom_tile())+geom_polygon(data=us_states,aes(x=long, y=lat, group=group), colour="black", fill="white", alpha=0)

How can I combine the two ggplot statements to overlay the grid of fake attribute data onto the map of the U.S. and Canada? Thank you for any advice.

Was it helpful?

Solution

This should do the job

library(ggplot2)
library(maps)

us = map_data("state")
# or this if you don't want the states' boundary
# us = map_data("states", boundary=FALSE)
ca = map_data("world", "Canada")

set.seed(1234)
xlim = c(-110,-100)
ylim = c(40,60)
dat_grid = expand.grid(x = xlim[1]:xlim[2], y = ylim[1]:ylim[2])
dat_grid$z = runif(nrow(dat_grid))

p = ggplot(aes(x=x,y=y,fill=z),data=dat_grid) 
p + geom_tile() + geom_polygon(data=us,aes(x=long, y=lat, group=group), colour="black", fill="white", alpha=0) + 
  geom_polygon(data=ca,aes(x=long, y=lat, group=group), colour="black", fill="white", alpha=0)

If you need Alaska:

library(ggplot2)
library(maps)

m = map_data("world2", c("usa", "Canada"))

set.seed(1234)
xlim = c(250,300)
ylim = c(40,60)
dat_grid = expand.grid(x = xlim[1]:xlim[2], y = ylim[1]:ylim[2])
dat_grid$z = runif(nrow(dat_grid))

p = ggplot(dat_grid,aes(x=x,y=y)) + geom_tile(aes(fill=z))
p  + geom_polygon(data=m,aes(x=long, y=lat, group=group), colour="black", fill="white", alpha=0) 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top