我想根据坐标将结构绘制到地图上。在我的数据集中,我有lat和long坐标,但我也有结构的ID(id)和导致观察结构(id_conn)的结构的ID。
我还想在“相关”点之间划一条线。例如:id为0的观测值只是一个点。但是id 101的观察具有id_conn 0,因此我希望在结构0和101之间存在一条线。
下面我有一些示例代码,我只是绘制结构,当然没有线条。如果我也应该在这里提供我的静态地图API密钥,我很抱歉 - 我认为这些是个人特定的。从示例数据集可以看出,创建的网络有时会重置为先前的ID,因此id_conn并不总是在前一个观察中找到的id。如果有人能在这里提供见解,我将不胜感激。
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)
你在找这样的东西吗?
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)
秘密成分是geom_segment()
,它允许您添加线段。您可以根据需要调整箭头的外观。