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

2023-12-23

我想为美国和加拿大的网格单元格着色。我的目标与这个问题非常相似:R 在地图上绘制填充的经纬度网格单元 https://stackoverflow.com/questions/11164974/r-plot-filled-longitude-latitude-grid-cells-on-map然而,这个问题只涉及美国,我不知道如何添加加拿大。

我能够通过修改此处的代码来绘制美国和加拿大的地图:https://groups.google.com/forum/#!topic/ggplot2/KAKhoE0GO4U 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/ 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)

这里是ggplot2之前的 Stack Overflow 帖子中使用的代码在 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将虚假属性数据网格覆盖到美国和加拿大地图上的语句?感谢您的任何建议。


这应该可以完成工作

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) 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

美国和加拿大的颜色网格单元 的相关文章

随机推荐

  • 没有编译器优化的 SSE 内在函数

    我是 SSE 内在函数的新手 并尝试通过它来优化我的代码 这是我的程序 用于计算等于给定值的数组元素 我将代码更改为 SSE 版本 但速度几乎没有改变 我想知道我是否以错误的方式使用SSE 此代码用于不允许我们启用编译器优化选项的分配 无
  • 当从 C# 程序中反序列化 JSON 时,我是否需要使用 JavaScriptSerializer 以外的任何东西?

    NET 中提供了 JavaScriptSerializer 类 System Web Script Serialization 命名空间 在 System Web Extensions dll 中提供 它最初旨在支持 AJAX Web 服务
  • 如何使用通配符设置docker的NO_PROXY

    正如 docker 官方文档中提到的here https docs docker com config daemon systemd configure where the docker daemon listens for connect
  • flatMap API 合约如何将可选输入转换为非可选结果?

    这是 Swift 3 0 2 中 flatMap 的合约 public struct Array
  • 从 Unity 中的 Android Studio 读取意图

    我有一个 Unity 游戏导出到 Android Studio 中 我有一个已保存游戏的列表 其中存储了玩家玩的每个游戏的最后一个场景 基本上存储玩家的进度 从 Unity 到 Android Studio 播放的最后一个场景的编写效果非常
  • Delphi 应用程序的插件系统 - bpl 与 dll?

    我正在编写delphi应用程序 它应该具有加载插件的能力 我使用 JvPluginManager 作为插件系统 管理器 现在 在新的插件向导中 他们说最好使用 bpl 类型插件而不是 dll 插件 这个解决方案与 dll 类型插件相比有什么
  • 增量求解有什么好处?

    如果 pop 完全破坏了上下文 即学到的引理 增量约束求解使用 堆栈 的目的是什么 模式 理由 我想如果我只有 1 个约束 几个 合词 最好进行单个查询 而不是 将单独帧中的合取词堆叠到堆栈上 如果我 有超过 1 个约束并决定使用增量求解
  • 如何使用 Gekko 加快优化速度?

    我的计划是优化家用电池的充电和放电 以最大限度地降低年底的电力成本 每15分钟测量一次家庭用电量 所以我在1天内有96个测量点 我想优化电池 2 天的充电和放电 以便第 1 天考虑到第 2 天的使用情况 我编写了以下代码并且它有效 from
  • new 类名(). 方法名(); VS className ref = new className();

    我遇到了我的同事在一个内部使用的代码eventListner 即 private void someActionPerformed java awt event ActionEvent evt new className methodNam
  • makefile“没有规则来创建目标”错误

    我已经研究这个问题有一段时间了 但仍然不知道出了什么问题 我的 makefile 如下所示 F90 pgf90 NETCDF DIR opt netcdf LBS L NETCDF DIR lib lnetcdff lnetcdf INCL
  • 通过交互和指南修改 ggplot2 中的图例

    df lt data frame Depth c 1 2 3 4 5 6 7 8 Var1 as factor c rep A 4 rep B 4 Var2 as factor c rep c C D 4 Value runif 8 g l
  • Eclipse 给出错误“...不是链接资源的有效位置。”

    当我尝试在 Eclipse 中为构建路径配置添加新的类路径变量 并且我添加的路径是当前工作区是其子目录的目录时 Eclipse 给出错误 C JavaStuff is not a valid location for linked reso
  • WCF DataContract - 标记成员 IsRequired=false

    我有一份合同如下 DataContract public class MyObj DataMember IsRequired true public string StrA get private set DataMember IsRequ
  • 具有非常大的 HDF5 文件的 Tensorflow-IO 数据集输入管道

    我有非常大的训练 30Gb 文件 由于我的可用 RAM 无法容纳所有数据 因此我想批量读取数据 我看到有 Tensorflow io 包实施了一种方式 https www tensorflow org io api docs python
  • 为什么Java类应该实现comparable?

    为什么是JavaComparable用过的 为什么有人要实施Comparable在课堂上 您需要实施比较的现实生活示例是什么 这是一个现实生活中的例子 注意String还实现了Comparable class Author implemen
  • 链表中的递归

    我一直在练习链表并想在其上实现递归 尽管在某些情况下我能够有效地实现它 但在其他情况下我却惨败 我想知道一种进行递归的方法 以便不必使用 while 来遍历链接列表 我已经使用递归来遍历数组 但是当我想在这种情况下做类似的事情时它失败 我在
  • Svg矩阵分解

    在 svg 中我们有方法element getCTM 它返回一个SVGMatrix as a c e b d f 0 0 1 我想从这个矩阵计算 sx sy 和旋转角度 关于这个主题有很多东西需要阅读和学习 我将给出一个基本的答案 但请注意
  • D3 - 显示/隐藏仅单击节点的文本

    我试图在单击时显示 隐藏 D3 中节点的文本 我尝试使用以下代码 var node svg selectAll node data json nodes node on click function if textShowing node
  • 如何手动更改 8086 的标志(在汇编代码中)?

    有没有办法手动更改每个标志 或者您是否必须使用一个您知道会改变它们的结果的命令 基本上我正在使用命令RCL 而且我不想一开始就得到1 所以我想改变CF到 0 我知道我可以使用如下命令 mov al 0 shl al 1 但我想知道是否有其他
  • 美国和加拿大的颜色网格单元

    我想为美国和加拿大的网格单元格着色 我的目标与这个问题非常相似 R 在地图上绘制填充的经纬度网格单元 https stackoverflow com questions 11164974 r plot filled longitude la