【问题标题】:Add North arrow and scalebar using ggsn使用 ggsn 添加指北针和比例尺
【发布时间】:2016-04-26 02:43:13
【问题描述】:

我正在寻找一种使用ggsn 包生成地图的方法,包括指北针和比例尺。

我已成功添加指北针,但还需要添加标量。

这是我到目前为止所做的:

mg  <- get_map(bbox(extent(geit_mb[[1]])*2), source="google", zoom=13)
bbox(extent(geit_mb[[1]])*2)
        min      max
s1 21.17131 21.25476
s2 69.85586 69.90750

maps <- ggmap(mg) + 
        geom_path(data = geit_4237, aes(x=location.long, y=location.lat)) +      
        scalebar(geit_4237, dist = 5, dd2km = TRUE, model = 'WGS84')

north2(maps, .75,.90)

我收到以下错误:

警告消息:已删除 2 行包含缺失值 (geom_text)。

我错过了什么?

但单独运行比例尺时,我会收到以下警告:

Warning messages:
1: In max(data$long) : no non-missing arguments to max; returning -Inf
2: In min(data$lat) : no non-missing arguments to min; returning Inf
3: In max(data$lat) : no non-missing arguments to max; returning -Inf
4: In min(data$lat) : no non-missing arguments to min; returning Inf
5: In max(data$lat) : no non-missing arguments to max; returning -Inf
6: In min(data$lat) : no non-missing arguments to min; returning Inf
7: In sin(lat) : NaNs produced
8: In cos(phi) : NaNs produced
9: In sin(phi) : NaNs produced
10: In sin(phi) : NaNs produced
11: In sin(lat) : NaNs produced
12: In cos(phi) : NaNs produced
13: In sin(phi) : NaNs produced
14: In sin(phi) : NaNs produced

输出:

 dput(bbox(extent(geit_mb[[1]])*2))


structure(c(21.17130645, 69.8558596, 21.25475825, 69.9075024), .Dim = c(2L, 2L), .Dimnames = list(c("s1", "s2"), c("min", "max")))

【问题讨论】:

  • 请让您的示例可重现。 geit_mb[[1]]的内容是什么?
  • geit_mb[[1]] 的内容是一个移动库对象,我在该对象上计算 ggmap 的 bbox 范围
  • 我重塑了我的问题。 bbox(extent(geit_mb[[1]])*2) 的输出是什么?
  • 最小最大 s1 21.17131 21.25476 s2 69.85586 69.90750
  • 您能否将dput(bbox(extent(geit_mb[[1]])*2)) 的输出添加到您的问题中?会让事情变得更容易。

标签: r maps ggmap


【解决方案1】:

如何在 ggmap 中添加比例尺并不是很明显。在阅读代码的过程中,我得到了很多帮助。

获取地图并加载库

library("ggmap")
library("ggsn")
map <- get_googlemap(center =c(23.89, 52.74), zoom = 12, maptype = "hybrid")

获取地图的边界框并将其重新格式化为data.frame for scalebar

bb <- attr(map, "bb")
bb2 <- data.frame(long = unlist(bb[c(2, 4)]), lat = unlist(bb[c(1,3)]))

将比例尺添加到 ggmap

ggmap(map) + 
  scalebar(data = bb2, dist = 1, dd2km = TRUE, model  = "WGS84", 
    location = "topleft", 
    anchor = c(
      x = bb$ll.lon + 0.1 * (bb$ur.lon - bb$ll.lon), 
      y = bb$ll.lat + 0.9 * (bb$ur.lat - bb$ll.lat)
    )
  )

data 是地图的边界框。

dist 给出比例尺每个部分的长度

dd2km 应该为 TRUE,因为地图数据是十进制度(但代码只是检查该值是否为空)

model 给出了谷歌地图的数据 - WGS84。

我发现单独设置location 会使刻度离边缘太近,以至于图例或条形图丢失了。因此,我使用anchor 将比例尺定位在距离左侧 10%、向上 90% 的位置。我仍然需要设置location,因为这与比例尺相对于锚点的定位方向有关。

【讨论】:

  • 优秀 :) 但是,做起来不应该这么麻烦。
猜你喜欢
  • 2018-10-24
  • 1970-01-01
  • 1970-01-01
  • 2018-09-29
  • 2016-11-28
  • 2020-08-31
  • 2017-08-13
  • 1970-01-01
  • 2020-01-25
相关资源
最近更新 更多