美国和加拿大的彩色网格单元

时间:2014-03-07 05:13:09

标签: r dictionary ggplot2 r-grid

我想在美国和加拿大为网格细胞着色。我的目标非常类似于这个问题:R Plot Filled Longitude-Latitude Grid Cells on Map但是,这个问题只针对美国,我无法弄清楚如何添加加拿大。

我可以通过修改此处的代码来绘制美国和加拿大的地图: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()

以下是为网格单元格创建虚假属性数据的代码,可在此处找到: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)

以下是早期Stack Overflow帖子中使用的ggplot2代码,用于覆盖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)

如何将两个ggplot语句组合在一起,将假属性数据网格覆盖到美国和加拿大的地图上?谢谢你的任何建议。

1 个答案:

答案 0 :(得分:4)

这应该做的工作

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)

如果你需要阿拉斯加:

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)