You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用R制作多国至墨西哥的旅行流向地图并添加连线?

问题:如何在R语言中添加各国到墨西哥的旅行流向连线

我是R语言新手,想要制作一张展示多个国家到墨西哥的旅行流向地图,所有数据流向均指向墨西哥。目前我已完成基础地图布局,但不知道如何添加各国与墨西哥之间的连线。

我的数据框

Country          Total       lon        lat
United Kingdom   5000       -3.43597    55.37805
China            4000       104.1954    35.86166
France           4000       2.213749    46.22764
Australia        4500       133.7751    -25.2744
Mexico           0          -102.553    23.6345

已完成的基础布局代码

world <- ne_countries(scale = "medium", returnclass = "sf")
class(world)
new_df <- read.csv('df.csv')
ggrepel::geom_label_repel

ggplot(data = world) +
  geom_sf()+geom_point(data=new_df, aes(x=lon, y=lat), colour='red')+
  ggrepel::geom_label_repel(data = new_df, 
                            aes(x = lon, y = lat), 
                            label = new_df$Country,
                            size = 2,
                            col = "blue")

解决方案

要添加各国到墨西哥的连线,核心是构造包含起点(各国)和终点(墨西哥)坐标的连线数据,再用ggplot2的连线图层绘制。

步骤1:提取墨西哥的坐标

先从数据框中单独提取墨西哥的经纬度,方便后续配对使用:

mexico_coords <- new_df[new_df$Country == "Mexico", c("lon", "lat")]

步骤2:构造连线数据

筛选出非墨西哥的国家数据,给每条数据添加墨西哥的终点坐标:

lines_df <- new_df[new_df$Country != "Mexico", ]
lines_df$lon_end <- mexico_coords$lon
lines_df$lat_end <- mexico_coords$lat

步骤3:添加连线到地图

提供两种连线样式供选择:

直线连线(geom_segment)

直接在原有代码中加入直线图层,还可以用旅行人数(Total)区分连线颜色:

ggplot(data = world) +
  geom_sf() +
  # 添加流向直线,用Total控制颜色深浅
  geom_segment(data = lines_df, 
               aes(x = lon, y = lat, xend = lon_end, yend = lat_end, color = Total),
               linewidth = 1, alpha = 0.7) +
  geom_point(data=new_df, aes(x=lon, y=lat), colour='red', size = 2) +
  ggrepel::geom_label_repel(data = new_df, 
                            aes(x = lon, y = lat), 
                            label = new_df$Country,
                            size = 2,
                            col = "blue") +
  # 添加颜色图例,标注旅行人数
  scale_color_viridis_c(name = "旅行人数") +
  theme_minimal()

曲线连线(geom_curve,更美观)

如果觉得直线生硬,可用曲线替代,通过curvature参数调整弯曲程度:

ggplot(data = world) +
  geom_sf() +
  # 添加流向曲线,curvature控制弯曲程度(正数右弯,负数左弯)
  geom_curve(data = lines_df, 
             aes(x = lon, y = lat, xend = lon_end, yend = lat_end, color = Total),
             linewidth = 1, alpha = 0.7, curvature = 0.2) +
  geom_point(data=new_df, aes(x=lon, y=lat), colour='red', size = 2) +
  ggrepel::geom_label_repel(data = new_df, 
                            aes(x = lon, y = lat), 
                            label = new_df$Country,
                            size = 2,
                            col = "blue") +
  scale_color_viridis_c(name = "旅行人数") +
  theme_minimal()

注意事项

  • 确保已安装并加载所需包,未安装的话先执行:
    install.packages(c("ggplot2", "sf", "rnaturalearth", "ggrepel", "viridis"))
    library(ggplot2)
    library(sf)
    library(rnaturalearth)
    library(ggrepel)
    library(viridis)
    
  • 可根据需求调整linewidth(线宽)、alpha(透明度)、curvature(曲线弯曲度)等参数优化视觉效果。

内容的提问来源于stack exchange,提问作者user19239587

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 09:54:33