【问题标题】:r ggmap - add annotation superimposed on mapr ggmap - 添加叠加在地图上的注释
【发布时间】:2018-10-10 01:30:42
【问题描述】:

存在关于地图图例编辑的问题 (e.g.),但不是我所需要的。

使用ggmap,如何选择地图中的点,并在地图上叠加标注?取以下代码:

Map <- get_map(location = 'Santiago, Chile', zoom = 6, maptype = "terrain")
Map <- ggmap(Map)
Points <- data.frame(lon=c(-71.82718,-71.31263),lat=c(-34.36935,-34.29322))
Map_Points <- Map + geom_point(data = Points,aes(x=lon,y=lat,size=6))

所以现在我有了一张漂亮的地图,上面有几个点。如何在其中一个点附近写一些注释?

【问题讨论】:

  • 您是指地图上某个点附近的注释吗?还是地图旁边的图例?
  • 我的意思是前者,在地图上叠加的一个点旁边的注释。 (注解和图例是不同的东西吗?)
  • 我的解释是,图例解释了数据的表示/编码,而注释是作为文本的数据。我认为您想在数据中添加带有标签的列,然后使用 geom_textgeom_label 之类的东西来放置您的注释。如果您想要干净的外观,您可以添加ggrepel,但不应该需要2分。 ggplot注释也可能有特定的paxkage,我认为它存在,即使我从未使用过。

标签: r ggplot2 ggmap


【解决方案1】:

很简单:

代码

library(ggrepel)                   # for the auto-repelling label
Map + 
    geom_point(data = Points, 
               aes(x = lon, y = lat),
               size = 3) +
    geom_label_repel(data = Points, 
                     aes(x = lon, y = lat, label = name),
                     size = 3,
                     vjust = -2,
                     hjust = 1)

数据

library(tmaptools)                 # for the geocode lookup
library(ggmap)
santiago_coords <- rbind(as.numeric(paste(geocode_OSM("Santiago, Chile")$coords))) 
Map <- get_map(location = santiago_coords, zoom = 6, maptype = "terrain")
Map <- ggmap(Map)
Points <- data.frame(lon=c(-71.82718,-71.31263),
                     lat=c(-34.36935,-34.29322),
                     name=c("Location One", "Location Two"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多