title: color grid cells in the United States and Canada
body: <p>I would like to color grid cells in the United States and Canada.  My goal is very similar to this question: <a href="https://stackoverflow.com/questions/11164974/r-plot-filled-longitude-latitude-grid-cells-on-map">R Plot Filled Longitude-Latitude Grid Cells on Map</a>  However, that question only deals with the United States and I cannot figure out how to add Canada.</p>
  
  <p>I was able to draw a map of the U.S. and Canada by modifying code found here: <a href="https://groups.google.com/forum/#!topic/ggplot2/KAKhoE0GO4U" rel="nofollow noreferrer">https://groups.google.com/forum/#!topic/ggplot2/KAKhoE0GO4U</a></p>
  
  <pre><code>library(ggplot2)
  library(rgeos)
  library(maps)
  library(maptools)
  
  PolygonCoords &lt;- function(polygon) {
    polygons &lt;- polygon@Polygons
    coords.list &lt;- lapply(seq_along(polygons), function(i) {
      # Extract the group, sequence, area, longitude, and latitude.
      coords &lt;- polygons[[i]]@coords
      cbind(i, 1:nrow(coords), polygons[[i]]@area, coords)
    })
    coords.df &lt;- as.data.frame(do.call(rbind, coords.list))
    names(coords.df) &lt;- c("order", "seq", "area", "long", "lat")
    return(coords.df)
  }
  
  ConvertWorldSimple &lt;- function(mapdata, min.area = 0) {
  
    coords.list &lt;- lapply(mapdata@polygons, PolygonCoords)
    ncoords &lt;- sapply(coords.list, nrow)
    coords.df &lt;- do.call(rbind, coords.list)
    coords.df$country &lt;- rep(mapdata@data$NAME, ncoords)
    country.group &lt;- factor(paste(coords.df$country, coords.df$order))
    coords.df$group &lt;- as.numeric(country.group)
    coords.df &lt;- coords.df[coords.df$area &gt;= min.area, ]
    return(coords.df)
  }
  
  data("wrld_simpl")
  world &lt;- ConvertWorldSimple(wrld_simpl, min.area = 0.1)
  
  world &lt;- world[world$country %in% c('United States', 'Canada'),]
  
  na &lt;- data.frame(
    country = c("United States", "Canada"),
    is.north.america = TRUE)
  
  world &lt;- merge(world, na, all.x = TRUE)
  world$is.north.america[is.na(world$is.north.america)] &lt;- FALSE
  
  world &lt;- 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()
  </code></pre>
  
  <p>Here is code to create fake attribute data for the grid cells, found here: <a href="http://www.numbertheory.nl/2011/11/08/drawing-polar-centered-spatial-maps-using-ggplot2/" rel="nofollow noreferrer">http://www.numbertheory.nl/2011/11/08/drawing-polar-centered-spatial-maps-using-ggplot2/</a></p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Here is the <code>ggplot2</code> code used in the earlier Stack Overflow post to overlay a grid of attributes on a map of the Lower 48:</p>
  
  <pre><code>library(ggplot2)
  library(maps)
  us_states &lt;- 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)
  </code></pre>
  
  <p>How can I combine the two <code>ggplot</code> statements to overlay the grid of fake attribute data onto the map of the U.S. and Canada?  Thank you for any advice.</p>