简体   繁体   English

基于ID的ggmap中的连接点

[英]Connecting points in ggmap based on an ID

I would like to plot structures onto a map, according to their coordinates. 我想根据其坐标将结构绘制到地图上。 In my dataset, I have lat and long coordinates, but I also have the structure's ID (id) and the ID of the structure that leads into structure under observation (id_conn). 在我的数据集中,我具有经度和纬度坐标,但我也具有结构的ID(id)和通向观察结构的结构的ID(id_conn)。

I would also like to have a line between "related" points. 我也想在“相关”点之间划一条线。 For example: the observation with id 0 would just be a dot. 例如:ID为0的观测值只是一个点。 But the observation with id 101 has id_conn 0, and thus I would like there to be a line between structures 0 and 101. 但是ID为101的观察值为id_conn 0,因此我希望结构0和101之间有一条线。

Below I have some example code where I just plot the structures, without the lines, of course. 在下面,我有一些示例代码,其中我仅绘制结构而没有线条。 I apologize if I'm also supposed to provide my Static Maps API key here - I think those are individual-specific though. 如果我还要在此处提供我的静态地图API密钥,我深表歉意-尽管我认为这些密钥是针对特定个人的。 One can see from the example dataset that the network that is created sometimes resets to a previous ID, so that id_conn is not always the id found in the previous observation. 从示例数据集中可以看到,有时创建的网络会重置为先前的ID,因此id_conn并不总是在先前的观察中找到的ID。 If anyone could provide insight here, I would appreciate it. 如果有人可以在这里提供见解,我将不胜感激。

install.packages("ggmap")
library(ggmap)

register_google(##your Static Maps API key##,
                account_type = "standard")

Gmap <- get_map(location = c(lon = 0, lat = 0), zoom = 9)

aux <- data.frame(
  id = c(0, 101, 102, 103, 104, 105, 106, 201, 202, 203, 204, 205),
  lat_coord = c(0, 0.1, 0.2, 0.3, 0.3, 0.4, 0.5, 0, 0, 0.1, 0, -0.1),
  lon_coord = c(0, 0.1, 0.2, 0.2, 0.3, 0.2, 0.2, 0.2, 0.3, 0.4, 0.4, 0.4),
  id_conn = c(NA, 0, 101, 102, 102, 103, 105, 101, 201, 202, 202, 202)
)

ggmap(Gmap) +
  geom_point(data=aux, aes(x=lon_coord, y=lat_coord)) +
  theme_void() +
  theme(legend.key = element_rect(fill = "black")) +
  coord_equal(ratio=1)

Are you looking for something like this? 您是否正在寻找这样的东西?

aux %>%
  inner_join(aux, by = c("id_conn" = "id")) %>%
  select(-id_conn.y) -> aux2


ggmap(Gmap) +
  geom_segment(data = aux2, aes(x = lon_coord.x, y = lat_coord.x, 
                                xend = lon_coord.y, yend = lat_coord.y), 
               color = "yellow", arrow = arrow(length = unit(0.2,"cm"))) +
  geom_point(aes(x=lon_coord.x, y=lat_coord.x),data=aux2) +
  geom_text(aes(x=lon_coord.x, y=lat_coord.x, label = id), data=aux2, hjust = -0.5) 

The secret ingredient is geom_segment() which allows you to add line segments. 秘密成分是geom_segment() ,它允许您添加线段。 You can adjust the appearance of the arrows as you like. 您可以根据需要调整箭头的外观。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM