将地图上的一个点与同一地图上的多个点连接的R ggplot [英] R ggplot connecting one point on a map with multiple points on the same map

查看:116
本文介绍了将地图上的一个点与同一地图上的多个点连接的R ggplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将美国地图上的一个位置与美国地图上的多个位置联系起来.

I am trying to connect one location on the map of the US with multiple locations on the map of the US.

library(tidyverse)
library(flights13)

首先,我选择2013年1月1日从纽瓦克机场(EWR)出发的所有航班,并获取目的地的地理坐标(如果在急需的机场中可用-并在顶部添加EWR的位置):

First, I am selecting all flights from Newark airport (EWR) on January 1, 2013 and grabbing the geographic coordinates for the destinations (if available in the tibble airports - and add EWR's location on top):

ewr <- flights %>% 
         filter(year== 2013, month== 1, day== 1, origin== "EWR") %>%  
         select(dest) %>% 
         distinct(dest) %>% 
         arrange(dest)

# Join it with airports database, add column "origin" filled with EWR:
mydest <- ewr %>% 
            left_join(select(airports, faa, lat, lon), 
                      by = c("dest" = "faa")) %>%
            filter(!is.na(lat)) %>% 
            select(lat, lon)
mydest

# Grab EWR coords:
ewrcoords <- airports %>% 
               filter(faa == "EWR") %>% 
               select(lat, lon)
ewrcoords
mydest <- rbind(ewrcoords, mydest)

然后,我想将EWR(纽瓦克)位置与所有目标位置连接起来.我尝试使用循环来执行此操作,但是在ggplot中无法正常工作:

Then, I want to connect the EWR (Newark) location with all the destination locations. I tried to do it using a loop, but it's not working inside ggplot:

mydest %>% 
  ggplot(aes(lon, lat)) +
  borders("state") +
  geom_point() +
  coord_quickmap() %>%
  for(i in 2:nrow(mydest)) {
    geom_line(data = mydest[c(1,i),], aes(lon, lat), 
              color = "black", size = 1)
  }

有没有可能做到这一点?或者我如何使循环工作? 非常感谢你!

Is it possible to do it without a loop? Or how could I make the loop work? Thank you very much!

推荐答案

尝试一下:

colnames(ewrcoords) = c("EWRlat", "EWRlon")
mydest <- cbind(ewrcoords, mydest)

mydest %>% 
  ggplot(aes(lon, lat)) +
  borders("state") +
  geom_point() +
  geom_segment(aes(y=EWRlat, x=EWRlon, xend=lon, yend=lat))+
  coord_quickmap() 

很抱歉,没有看到eipi10已经在评论中了.

sorry did not see it was already in the comment by eipi10.

这篇关于将地图上的一个点与同一地图上的多个点连接的R ggplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆