在 R / ggplot 中向中心点 (Lat/Long) 添加线

Adding Lines to a Central Point (Lat/Long) in R / ggplot

我有一些数据是 Lat/Long 供参加活动的客人使用的(我在下面添加了数据,只是前几行,但我的 df 更大)。

Name, n, Town, State, Country, Lat, Long
Bob, 5, Dundee, n/a, Scotland, 56.462018, -2.970721
Bob, 1, Forfar, n/a, Scotland, 56.643558, -2.889062
Jefferson, 1, Inverness, n/a, Scotland, 57.477773, -4.224721
Dave, 2, London, n/a, England, 51.507351, -0.127758
Randy, 2, Dublin, n/a, Ireland, 53.349805, -6.260310
Buck, 2, Landing, NJ, USA, 40.905411, -74.665695
John, 2, Randolph, NJ, USA, 40.847806, -74.574725
Frank, 1, Morris Plains, NJ, USA, 40.839592, -74.481870
Jimmy, 1, Henryville, PA, USA, 41.093703, -75.241293
Mike, 1, Cliffside Park, NJ, USA, 40.821489, -73.987639
Spence, 1, North Bergen, NJ, USA, 40.804267, -74.012084
Jimmy, 1, West New York, NJ, USA, 40.787879, -74.014306
Jerry, 2, North Bergen, NJ, USA, 40.804267, -74.012084
Dingle, 1, North Bergen, NJ, USA, 40.804267, -74.012084

然后我有活动地点的Lat/Long:

lat<-40.853988
long<--74.829055

我想做的是为客人绘制所有点作为散点图,然后用一条线从每个客人点到活动地点。

获取散点图很简单:

### Copy Data from above to Clipboard
event<-read.table(file = "clipboard", sep = ",", header=TRUE)
ggplot(event, aes(x=Long, y=Lat)) + 
  geom_point(color="darkred", size=2, alpha=.3) + 
  geom_point(aes(x=long, y=lat), color="blue", size=3)

现在,有没有办法让每个客人点(红点)到活动地点(蓝点)的线?

我尝试了 geom_path() 但这系统地添加了一条点对点的线。

这样的事情怎么样。它所做的是为不同的组模拟 geom_path 的结构,其中每个组是原始 df

的一个点
require(dplyr)
require(ggplot2)
EventCoords <- event %>% mutate(Lat=lat, Long=long,order=2,group=1:nrow(event))
event <- event %>% mutate(group=1:nrow(.),order=1) %>% rbind(EventCoords)
P1 <- ggplot(event, aes(x=Long, y=Lat)) +
     geom_point(color="darkred", size=2, alpha=.3) +
     geom_point(aes(x=long, y=lat), color="blue", size=3)
P1 <- P1 + geom_path(aes(group=group))

您可以使用 geom_segment。首先将目的地lat/long添加到数据库

lat<-40.853988
long<--74.829055
event$lat1 <- lat
event$long1 <- long

ggplot(event, aes(x=Long, y=Lat)) + 
  geom_point(color="darkred", size=2, alpha=.3) + 
  geom_point(aes(x=long, y=lat), color="blue", size=3) +
  geom_segment(aes(xend = long1, yend = lat1))