【问题标题】:color grid cells in the United States and Canada美国和加拿大的彩色网格单元格
【发布时间】:2014-03-07 05:13:09
【问题描述】:

我想为美国和加拿大的网格单元格着色。我的目标与这个问题非常相似: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 语句将虚假属性数据网格叠加到美国和加拿大的地图上?谢谢你的建议。

【问题讨论】:

    标签: r dictionary ggplot2 r-grid


    【解决方案1】:

    这应该可以完成工作

    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) 
    

    【讨论】:

    • 这非常有帮助。我不想再要求了。虽然,如果地图可以以陆地为中心,那将是理想的。
    • 这很容易,最后+ xlim(175, 320) + ylim(20, 90)
    猜你喜欢
    • 2014-04-17
    • 1970-01-01
    • 2014-05-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-03
    • 1970-01-01
    • 1970-01-01
    • 2011-05-05
    相关资源
    最近更新 更多